Preg_replace_callback storing in array

Vintage12

I'm looking to store every match found using preg_replace_callback in an array which is to be used at a later point. This is what I have so far and I can't work out what is wrong, currently it is storing the last match found, in both $match[0] and $match[3].

What I am attempting to achieve overall is to replace every match with a hyperlinked number, then print the full text below it. Each number is to be linked to its corresponding text.

            global $match;
            $match = array();
            $pattern = $regex;
                $body = preg_replace_callback($pattern, function($matches){
                    static $count = 0;
                    $count ++;
                    $match = $matches;
                    return "<a href=\"#ref $count\">$count</a>"; 
                }, $body);
Barmar

You need to put the global statement inside the function. You also need to push a new element onto the $match array, not overwrite it. And I doubt you want a space between #ref and $count in the href attribute.

$match = array();
$pattern = $regex;
$body = preg_replace_callback($pattern, function($matches){
    global $match;
    static $count = 0;
    $count ++;
    $match[] = $matches;
    return "<a href=\"#ref$count\">$count</a>"; 
}, $body);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

preg_replace to preg_replace_callback with an array as replacement

From Dev

preg_replace to preg_replace_callback with an array as replacement

From Dev

preg_replace_callback(): Requires argument 2, 'Array', to be a valid callback

From Dev

preg_replace_callback store match in global array and replace

From Dev

Allow write access to array in PHP preg_replace_callback

From Dev

preg_replace_callback regex issue, match with (.*?) returns array

From Dev

Using preg_replace_callback

From Dev

Codecolorer - preg_replace_callback()

From Dev

Preg_replace and Preg_replace_callback

From Dev

PHP: Storing callback functions with arguments on array

From Dev

Replace 'e' modifier with preg_replace_callback

From Dev

Replace deprecated preg_replace /e with preg_replace_callback

From Dev

Replace preg_replace() to preg_replace_callback()

From Dev

PHP preg_replace_callback ignore id

From Dev

preg_replace_callback: Unknown modifier '!'

From Dev

Replacing /e modifier with preg_replace_callback

From Dev

preg_replace_callback not working in php

From Dev

preg_replace_callback returns duplicate values

From Dev

Php, Regex preg_replace_callback

From Dev

Using preg_replace_callback with shortcodes

From Dev

sort properties with preg_replace_callback

From Dev

Php, Regex preg_replace_callback

From Dev

Replacing /e modifier with preg_replace_callback

From Dev

preg_replace_callback with Spoilers into Spoilers

From Dev

How to use preg_replace_callback

From Dev

preg_replace_callback - compilation failed

From Dev

Get opposite match with preg_replace_callback

From Dev

PHP REGEX preg_replace_callback not matching

From Dev

convert preg_replace to preg_replace_callback modifier error

Related Related

HotTag

Archive