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
vote
2 answers

PHP preg_replace_callback with unicode

I wrote a simple script below to simulate my problem. Both my string and pattern contain unicode characters. Basically, if I run it from command line (php -f test.php), it prints "match" as expected. But if I run it through web server (apache,…
Formosan
  • 21
  • 3
1
vote
4 answers

PHP Regex Replace Image SRC Attribute

I'm trying to find a regular expression that would allow me replace the SRC attribute in an image. Here is what I have: function getURL($matches) { global $rootURL; return $rootURL . "?type=image&URL=" .…
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
1
vote
1 answer

Applying function on second or third term in preg_replace (preg_replace_callback?)

I have below a simple (BBCode) PHP code for insert code into a comment/post. function highlight_code($str) { $search = array( '/\[code=(.*?),(.*?)\](((?R)|.)*?)\[\/code\]/is', …
sooper
  • 5,991
  • 6
  • 40
  • 65
1
vote
2 answers

Replacing end div tags using preg_replace_callback function

I am trying to develop a PHP script that replaces all divs in an HTML string with paragraphs except those which have attributes (e.g.
). The first thing my script currently does is use a simple str_replace() to replace all occurrences of…
siberiantiger
  • 305
  • 1
  • 3
  • 10
1
vote
2 answers

how to replace the hashtagged text in sequence using preg_replace(_callback)

the target text is something like: Lorem ipsum dolor #sit# amet, consectetur #adipisicing# elit, sed do #eiusmod# tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut…
Ren Ji
  • 115
  • 7
1
vote
3 answers

php - Clean user input using preg_replace_callback and ord()?

I have a forum style text box and I would like to sanitize the user input to stop potential xss and code insertion. I have seen htmlentities used, but then others have said that &,#,%,: characters need to be encoded as well, and it seems the more I…
1
vote
1 answer

500 error preg_replace_callback when using function ($match) use ($video){}

Works on my localhost running php 5.3.4 Does not work on my VPS hostgator account running 5.3.6 The problem here seems to be the "use" keyword Code Below Video; $post['Post']['body'] = …
styks
  • 3,193
  • 1
  • 23
  • 36
1
vote
0 answers

How can I restrict preg_replace_callback so that it replaces a limited number of expression matches?

An example of what I need: $input = "bar bar bar bar bar"; $result = preg_replace_callback('bar', 'foo', $input); // And there must be some way to limit preg_replace_callback so that it replaces only the first 2 (for example) matches of the…
pseudonym1
  • 11
  • 1
1
vote
1 answer

preg_replace_callback not working, function not working

i am using something like the following but the function replace_callback isnt executed at all $body2 = preg_replace_callback("/\{(.*)\}/isUe","replace_callback",$body); the string $body looks like this:…
Martin Huwa
  • 401
  • 3
  • 7
  • 11
1
vote
1 answer

Removing line breaks between 2 different character sequences

I'm editing a csv file which contains hidden line breaks. When I apply the following php script, the line breaks are successfully removed from the entire file. $csvFileNew = str_replace(array("\r", "\n"), '', $csvFileOld); But I only want to remove…
mojobullfrog
  • 189
  • 3
  • 11
1
vote
3 answers

preg_replace_callback() memory issue

i'm having a memory issue while testing a find/replace function. Say the search subject is: $subject = "I wrote an article in the A+ magazine. It'\s very long and full of words. I want to replace every A+ instance in this text by a link to a…
pixeline
  • 17,669
  • 12
  • 84
  • 109
1
vote
0 answers

Regex Template Parser ignore new lines

I am trying to build a "simple" RegEx parser, to generate some templates in my project. I have made this bit of regex to match and parse loops - which seems to be correct and finding everything I want, and my loop replaces the values. The last bit I…
OnIIcE
  • 811
  • 9
  • 27
1
vote
1 answer

Regex : ignore HTML Tags with preg_replace_callback

I'm trying to grab all text between the HTML Tags (if there), and put a function on it .. i mean.. my code now is $code = preg_replace_callback('/(\(\s*\')\s*(.*?)\s*(\')/', function($matches) { return strtolower($matches); …
Abdullah Raid
  • 545
  • 1
  • 4
  • 14
1
vote
1 answer

preg_replace with urlencode

I am trying to create hashtag links with preg_replace and I encounter issues when a "" in the hashtag is present. I am not really good at patterns so any help would be appreciated: My pattern: $hashtags_url =…
Sergiu
  • 73
  • 7
1
vote
1 answer

php dynamic replacement with a std object

How can I make a dynamic replacement with a std object? I can't get how to use $1 in this case :( See below. $lang->custom_name = "Me"; $lang->custom_email = "Me@me"; $html = "hello {{custom_name}} with {{custom_email}} "; $html =…
rosia
  • 219
  • 1
  • 2
  • 6