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
0
votes
1 answer

replace text in associative array with text from another array

I have a array like so $tags = Array ( [0] => Array ( [0] => [first_name] [last_name] [2] => [city],[state] [zipcode] ) ) i also have a list like so $array_list = [0] => Array ( [first_name] => Bob …
Yeak
  • 2,470
  • 9
  • 45
  • 71
0
votes
0 answers

preg_replace_callback vs while loop line by line for template compiler

i built php class for parsing html templates , and i used preg_replace_callback function for parsing template while loops & if conditions , for loop , ... etc i repeated the function many times , for every process , such as one time for parsing…
Jason4Ever
  • 1,439
  • 4
  • 23
  • 43
0
votes
1 answer

Replace and extract matches with regex

How can I get all matches with for example preg_match_all() and also replace the results with an empty string? I've tried stuff like this:
MichaelBS
  • 15
  • 3
0
votes
0 answers

preg_replace_callback finding 2 matches should be 1

I have the following code being shown in a textbox and am turning it into a table: foreach([-Order Detail List([-Order Id-])-]){{ [-order_detail_id-] [-order_id-] [-account_id-] [-order_detail_assigned-] }} I'm doing the replace…
Dom
  • 858
  • 2
  • 11
  • 30
0
votes
1 answer

PHP preg_replace_callback not replacing my "matches"

Why is this returning the original string and not the edited string? Original String: I cant believe Ed ate [-food-] for breakfast. Replace: preg_replace_callback('/\[\-[a-zA-Z0-9_]\-\]/', 'tag_func', $email_body); function tag_func($matches){ …
Dom
  • 858
  • 2
  • 11
  • 30
0
votes
1 answer

preg_replace_callback: regular expression search and replace

$details = "text...[book=123]...text..."; $details = preg_replace_callback( "/\[book=(.+?)\]/smi", function ($m) { global $skip_books; $book = $m[1]; // 123 $feed = $m[2]; // 456 return "
richard
  • 1,456
  • 4
  • 15
  • 22
0
votes
2 answers

preg_replace_callback pattern issue

I'm using the following pattern to capture links, and turn them into HTML friendly links. I use the following pattern in a preg_replace_callback and for the most part it works. "#(https?|ftp)://(\S+[^\s.,>)\];'\"!?])#" But this pattern fails when…
mattauckland
  • 483
  • 1
  • 7
  • 17
0
votes
1 answer

regex extract/replace values from xml-like tags via named (sub)groups

Trying to create a simple text-translator in PHP. It shoult match something like: Bla bla {translator id="TEST" language="de"/} The language can be optional Blabla Here is the code: $result = preg_replace_callback( …
void
0
votes
3 answers

preg replace_callback mentions and hashtags in post

Lets say i have a text, $text = '@stackguy @flowguy I need to learn more advanced php #ilovephp'; I want to replace both @stackguyand @flowguy with those 2 anchor tags respectively. This should also work for any number of @'s in the text string.
Saff
  • 563
  • 1
  • 10
  • 21
0
votes
2 answers

Regex: faulty syntax used with preg_replace_callback?

I have borrowed code from this link PHP regex templating - find all occurrences of {{var}} to implement a means of applying values to template fiies. This uses the preg_replace_callback() function my preferred method of naming is…
vfclists
0
votes
1 answer

String containing {{calendar}} but need it to call calendar.php

I'm storing HTML content in a MySQL field with a text data type, and I'm using ckeditor as a WYSIWYG editor to create the HTML that is being stored in MySQL. I'm looking for a way for a user to put some kind of string that I could look for and…
user520300
  • 1,497
  • 5
  • 24
  • 46
0
votes
1 answer

preg_replace_callback with PHP 5.2.17

I have this code and its working fine with PHP 5.3 onwards but i need to run it from 5.2.17 onwards please anybody help me with this. $data = array('title'=>'some title', 'date'=>1350498600, 'story'=>'Some story'); $template = "#title#,…
alladeen
  • 13
  • 5
0
votes
1 answer

preg_replace_callback - do twice

Yo, i'm trying to do this script working, but it doesn't work. How do i do it twice, the preg_replace_callback with two different functions. Thanks! function prepend_proxy($matches) { $url = (substr($_GET['url'], 0, 7) == 'http://') ?…
Sara
0
votes
2 answers

php: Why preg_replace_callback does not allow built in php functions, just anonymous user functions?

This does not work, and outputs an empty string: $check["pattern"] = "correct"; $text = "Could this be correct?"; echo preg_replace_callback($check["pattern"],ucfirst,$text); Would have been nice to use built in functions. In fact general callbacks…
giorgio79
  • 3,787
  • 9
  • 53
  • 85
0
votes
2 answers

preg_replace_callback: passing in replacement callback as a variable not working

This does not work $check["pattern"] = "/correct/"; $callback = "function ($m) { return ucfirst($m[0]);}"; echo preg_replace_callback($check["pattern"],$callback,"correct" ); output: correct This works $check["pattern"] =…
giorgio79
  • 3,787
  • 9
  • 53
  • 85