Questions tagged [preg-replace-callback]

preg_replace_callback() is a PHP function that uses regular expressions to find substrings and a PHP callback function to perform the replacement.

preg_replace_callback replaces the older (and insecure) /e flag in preg_replace, where PHP would eval the replace string and execute the PHP contained inside. preg_replace_callback uses a direct function call instead, which negates that risk entirely.

The most common use is where you need to use a regular expression to find strings, but you need a PHP function to transform that string. Consider the below function, which finds instances of Bob and makes them all uppercase. This example passes an anonymous function (PHP >= 5.3) but you can pass the function name instead.

echo preg_replace_callback('/Bob/', function($match) {
    return strtoupper($match[0]);
}, 'We like Bob');
// outputs We like BOB
412 questions
-1
votes
3 answers

Replace with same named PHP variable

My Example: $name = "Simon"; $string = "My name is [name]."; echo preg_replace("/\[(.*)]/", ${"$1"}, $string); // Expected: My name is Simon. // I get: My name is . // ${"$1"} should be $name? exit(); When I do only: echo preg_replace("/\[(.*)]/",…
-1
votes
1 answer

How can I replace preg_replace with preg_replace_callback in PHP5.4?

I have a PHP page in which I am using following command:- $abc = preg_replace('/&#(\d+);/me', "chr(\\1)", $abc); Now I want to replace the above command with the "preg_replace_callback" function as preg_replace is deprecated in PHP 5.4 . How can I…
Gaurang
  • 371
  • 2
  • 4
  • 21
-1
votes
1 answer

preg_replace_callback pattern not containing certain sub-string

Currently i am using the following code to modify content between {| and |} using a custom function parse_table $output = preg_replace_callback("({\|(.*?)\|})is", function($m) {return parse_table($m[1]);}, $input); Now i want to modify it such that…
aye
  • 301
  • 2
  • 16
-1
votes
1 answer

How can I replace the code with preg_replace_callback()? (/e)

$in = '(<(/?(?:strong|p|em|a|ol|ul|li|img|iframe)\b.*?)>)ie'; $contenu = preg_replace($in, "'<'.html_entity_decode('$1',ENT_QUOTES,'UTF-8').'>'", $contenu);
-1
votes
2 answers

Replace deprecated preg_replace with preg_replace_callback

code below gives a deprecation warning after upgrading to PHP 5.5+ $sentence=preg_replace('/~([^<>]{1,})~/e', "''.UTF8_strtoupper('\\1').''", $sentence); Deprecated: preg_replace(): The /e modifier is deprecated,…
jones
  • 631
  • 3
  • 9
  • 20
-1
votes
1 answer

preg_replace_callback() not replace pattern with a function in class

I ve a function in a class that replaces patterns. part of the function is below. $text = preg_replace_callback('/\[dekanlik\]/',function (){return $this->dekanlik();},$text); dekanlik() is another function in that class which lists the members.…
JuST4FuN
  • 1
  • 1
-1
votes
1 answer

PHP: call-time pass-by-reference broke my array_walk and I can't fix it

Here is the PHP code before deprecation errors were introduced: array_walk( $tags, 'shortcode_tag_parse', &$content ); function shortcode_tag_parse( &$tag, $key, &$content ) { $content = preg_replace_callback(…
Nahydrin
  • 13,197
  • 12
  • 59
  • 101
-1
votes
1 answer

Php preg_replace_callback no work me

I try replace characters from string and put others , for this use preg_replace_callback function , the problem is this function change order of original text and show differents results For example if the string it´s : Hello everybody [1-2-3] this…
Robert
  • 315
  • 2
  • 5
  • 9
-1
votes
1 answer

PHP: preg_replace_callback changes the order of the result

I have the following code return preg_replace_callback( "#\{gallery: '(.+?)'(?: dir: ([0-1]))?\}#i", create_function('$i', 'echo $i[1];' ), $string); My problem is that if my string looks like this: top {gallery:…
Waldir Bolanos
  • 422
  • 3
  • 7
-2
votes
1 answer

Regex fix for preg_replace_callback function

STRING : $string = '{$string#anything#something this string will output default |ucfirst|strtoupper}'; PREG_REPLACE_CALLBACK CODE(PHP) : $string = preg_replace_callback('/\{\$?([^# ]+)\#?([^ ]+)? ?([^|]+)?[ \|]?([^\}]+)?\}/', $matches,…
Ranbir Kapoor
  • 95
  • 1
  • 1
  • 6
-2
votes
3 answers

preg_replace_callback_array Doesn't return match calculation value

I try preg_replace_callback_array for math bbcode. Its returning as plain text while i require calculated result function($matches) {$value = "$matches[1]"; return…
OH Pavel
  • 31
  • 6
-2
votes
1 answer

preg_replace_callback create_function is deprecated

i have the following function for replace variables in a string function replace_variables($string,$variables) { return preg_replace_callback('/{\$([A-Za-z_]+)\}/', create_function ('$matches', 'return $$variables[1];'),…
ntan
  • 2,195
  • 7
  • 35
  • 56
-2
votes
1 answer

How to replace symbol to letter

I've example text: $text = "I've got a many web APP=. My app= is not working fast. It'= slow app="; It is necessary to replace the symbol "=" with the letter "s" by the following condition: If the right or left with the "=" sign has a letter then…
John
  • 468
  • 3
  • 16
-2
votes
1 answer

Searching text file in PHP

Trying to search a text file for a string and replace it with HTML code that is either an external file reference OR an anchor/bookmark within the same file. Have added a diagram. Blue Lines : If a file with the corresponding name exists, then…
TimJ
  • 27
  • 3
-2
votes
2 answers

Issue with preg_replace_callback_array

The function below checks for multiple regex parsed but its not working and i couldn't spot where am getting it wrong. function getig($string) { $string = preg_replace_callback_array( [ "/\[instagram=(.+?)\]/" => function($matches) { …
Omotayo
  • 59
  • 6
1 2 3
27
28