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

php regex preg_replace_callback fails to handle mutliple lines

I have a code like $newstr = preg_replace_callback("/

(.+?)

/", function($matches) { return str_replace($matches[1], '' . $matches[1] . '', $matches[0]); }, $str); It replaces if a single line string is given like this …
user3182138
0
votes
2 answers

Add comments and attributes including an incremented number to elements in an HTML string

I have been trying to understand how preg_replace_callback() works, but I just don't get it. Say for example, I get_contents from navigation.php. In that text are a bunch of a href and divs and I want to give incremental ids to and add in some code…
Fred Turner
  • 139
  • 1
  • 9
0
votes
2 answers

Can't get preg_replace_callback function working

It won't work, if you see anything wrong.. function replaceLink($matches){ $final = '\''.$data['alt'].'\'/'; return $final; } $message = preg_replace_callback('#\[img\]([1-3])\[/img\]#isU',…
0
votes
1 answer

simple regex isn't working using preg_replace_callback()

I'm trying to use preg_replace_callback to fill in variables in an imported document (which I control), based on this answer, but it's not working. As far as I can tell, the callback is never invoked, which would mean the regex is never being…
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
0
votes
1 answer

preg_replace_callback with case insensitive

I have an array $bad_words containing English rude words and their dotted out equivalents and I use preg_replace_callback as follows: $bad_words = array( "badword" => "s••t", "badword2" => "f••k" ... ); function…
John Doe
  • 983
  • 4
  • 14
  • 27
0
votes
2 answers

How to automatically convert the tabs in the pasted code(from .net, netbeans, notepad,sublime) to nbsp; in php

So I have this blog which I wrote in php where I post articles regarding programming and embedded systems, its not so famous but I am doing this in a hope that it will serve as a leverage in interviews, I am in final year of college. I used to…
Aditya
  • 1,240
  • 2
  • 14
  • 38
0
votes
1 answer

preg_replace I want to replace text in Chinese + English inside the quotes

I want to replace text in Chinese + English inside the quotes: sample source text: emMsg('您的php版本过低,请选用支持PHP5的环境安装english word。'); or (程序不会自动创建数据库,请提前创建一个空数据库或使用已有数据库) preg_replace("/(?:[(]([\p{Han}]+)[)] )?/ux", '-', $val); but the result is…
0
votes
1 answer

Regex in preg_replace_callback error. PHP

I am trying to create a web application that will convert any selected webpage into a form of simple english. I have a word for word translation stored in a My_SQL database. I have this code so far. It works but only seems to do what i want it too…
0
votes
1 answer

str_replace inside a preg_replace_callback not working.

I am trying to use a str_replace in this. When i use a array("","") it works and replaces as required, however when i use a predefined array, in this case fetched from a my_sql database it dosnt seem to work. Also the str_replace works with the…
0
votes
2 answers

Remove a string from regex match results (remove link class if link contains a specific attribute)

I'm trying to get my PHP code to remove class="something" from links that contain the attribute data-class="false" and leave other links alone. $mytext = 'text text text link1 text text text…
C-Dog
  • 125
  • 9
0
votes
3 answers

How to replace text with a regex pattern and integrate a counter in the replacement text?

function parse($string){ $counter = 0; $string = preg_replace("_\[b\](.*?)\[/b\]_si", ''. $counter .'. $1', $string, -1, $counter); return $string; } I'm trying to make a ubb parser, that parses tags and…
Thew
  • 15,789
  • 18
  • 59
  • 100
0
votes
1 answer

How is the position of a subject array pointer determined in a preg_replace_callback's callback function?

How is the position of a $subject_array pointer determined in a preg_replace_callback's callback function? I.e. What is the numeric key? e.g. $final_array = preg_replace_callback("/pattern/", create_function( '$matches', '[WHAT…
zylstra
  • 740
  • 1
  • 8
  • 22
0
votes
1 answer

RegEx, preg_replace_callback Question PHP

This is what I've got for for my RegEx, I was wondering if this is the best way. I want to be able to find something similar regardless of the spacing between Identifiers and not be case sensitive. And if possible, not worry about order.. …
Brad
  • 2,237
  • 5
  • 41
  • 69
0
votes
1 answer

Preg_Replace_Callback replacing matched items with Array

I created a replace function to identify twitter accounts and I want to create a link to my internal pages if I have that twitter account present in my data. //Creates a link for users $tester = preg_replace_callback( '/\s+@(\w+)/', …
Ben Gummelt
  • 101
  • 3
0
votes
2 answers

String Replacements from Values in Assoc Array

I'm given a string of HTML with some "tokens" in it. They are structured like {:TOKEN_NAME:} For instance: