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
4
votes
2 answers

Add variable's value after each sequence of letters in a string using preg_replace_callback()

How can I pass a variable into the custom callback function of a preg_replace_callback() call? For example, I'd like to use the variable $add within my callback function: private function addToWord($add) { return preg_replace_callback( …
R_User
  • 10,682
  • 25
  • 79
  • 120
4
votes
1 answer

Find and replace URLs in a blob of text but exclude those in link tags

I have been trying to run through a string and find and replace URLs with a link, here what I have come up so far, and it does seem to work for the most part quite well, however there are a few things I'd like to polish. Also it might not be the…
Czechmate
  • 41
  • 2
4
votes
1 answer

URL detection and BB-Style tags (regex, look-ahead issue)

so I'm building a small CMS and I'd like to avoid allowing HTML in the content editor. For that reason I want to detect raw URLs in text aswell as supporting BB-like tags, for better customization. www.example.com [link http://www.example.com]Click…
psalz
  • 174
  • 7
4
votes
5 answers

How To Replace First HTML Tag in PHP

I have a text string in PHP: MOST of you may have a habit of wearing socks while sleeping. Wear socks while sleeping to prevent cracking feet Socks helps to relieve sweaty feet We can see, the…
Riyanto Wibowo
  • 364
  • 1
  • 6
  • 13
4
votes
1 answer

preg_match for certain part of the comments

i want to find certain part of the comments. following are some examples /* * @todo name another-name taskID */ /* @todo name another-name taskID */ currently im using following regular expression '/\*[[:space:]]*(?=@todo(.*))/i' this returns…
Basit
  • 16,316
  • 31
  • 93
  • 154
3
votes
3 answers

PHP Preg Replace replacement array

Ok so I'm trying to do something like so: preg_replace("/\{([a-zA-Z0-9_]+)\}/", $templateVariables[$1], $templateString); Now I know that is not possible like it is, however I would like to know if there is a way to do this, because I have tried to…
csteifel
  • 2,854
  • 6
  • 35
  • 61
3
votes
3 answers

Replacing all occurences of a specific word which are not enclosed with the words OPEN and CLOSE?

I have the following string: OPEN someone said hello CLOSE im saying hello people OPEN some said hello OPEN they said hello again CLOSE i have to go now though CLOSE hello again! I'm trying to match all occurences of hello (that are not enclosed…
user962026
  • 207
  • 1
  • 5
  • 12
3
votes
2 answers

PHP preg_replace_callback creates false entries in matches for named groups

I have a couple of "shortcode" blocks in a text, which I want to replace with some HTML entities on the fly using preg_replace_callback. The syntax of a shortcode is simple: [block:type-of-the-block attribute-name1:value attribute-name2:value…
3
votes
4 answers

Preg_replace or preg_replace_callback?

I have links on some pages that use an old system such as: This is a link They need to be converted to the new system which is like: This is a link I can use…
james
  • 73
  • 1
  • 7
3
votes
2 answers

preg_replace with multiple patterns replacements at once

I have few substitutions to apply on my $subject but I don't want to allow the output from old substitutions #(1 .. i-1) to be a match for the current substitution #i. $subject1 = preg_replace($pat0, $rep0, $subject0); $subject2 =…
mmonem
  • 2,811
  • 2
  • 28
  • 38
3
votes
1 answer

Replace curly braced expression with one item from the expression

Here is a sample string: {three / fifteen / one hundred} this is the first random number, and this is the second, {two / four} From the brackets, I need to return a random value for example: One hundred is the first random number, and this is the…
alex
  • 524
  • 2
  • 11
3
votes
4 answers

Replace placeholders in text by referencing a translation variable

I'm trying to remove eval from the following function. I tried with sprintf and ${} , but still cannot find a solution. Here the function: function parseDbString(string $value = 'Looking for a good {{ $pippo }}'){ $pippo='Pizza'; return…
Vixed
  • 3,429
  • 5
  • 37
  • 68
3
votes
1 answer

Find all matches between delimeters

How do I replace all

tags between delimiters? My text is

other text...

other text...

``text text...

text text...

text text ...``

other text...

other text...

``text text...

text…

loneFinder
  • 33
  • 3
3
votes
1 answer

How to implement my algorithm text correction for the replacement of words in the text?

Brief Help me to create a new function or change the function correct() so that the result works in a case-insensitive manner for the input text. Example Usage Example usage for the correct() method: $text = "Точик ТОЧИК точик ТоЧиК тоЧИК"; $text…
John
  • 468
  • 3
  • 16
3
votes
1 answer

Warning: preg_replace_callback(): Requires argument 2

I just updated PHP on my server from php 5 to php 7 and I'm getting these warnings: Warning: preg_replace_callback() [function.preg-replace-callback0]: Requires argument 2, 'chr(\1)', to be a valid callback Warning: preg_replace_callback()…
Matei Zoc
  • 3,739
  • 3
  • 16
  • 18
1
2
3
27 28