Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP replace all instances with single regex pattern

Tags:

regex

php

I have a single regex and I want to replace each match in the array of matches with a corresponding array of replacements in the most efficient way possible.

So for instance, I have:

$string = '~~hello~~ there, how ~~are~~ ~~you~~?';

$pattern = '/~~(.*?)~~/';

$replacements = array();
$replacements[0] = 'hi';
$replacements[1] = 'am';
$replacements[2] = 'i';

and I want to turn $string into:

hi there, how am i?

Initially I hoped it'd be as simple as:

$string = preg_replace($pattern, $replacements, $string);

but it doesn't seem to work. So the first question is: if $replacements is an array, then does $string must also be an array?

Now, I can come up with (seemingly) inefficient ways to do this, like counting the number of matches and making an array filled with the appropriate number of identical regexes. But this leads us into question two: is there a more efficient way? How would you do it, PHP pros?

like image 413
jamesplease Avatar asked Nov 17 '12 03:11

jamesplease


People also ask

What does preg_ replace() Return?

The preg_replace() function returns a string or array of strings where all matches of a pattern or list of patterns found in the input are replaced with substrings. There are three different ways to use this function: 1. One pattern and a replacement string.

What is the difference between Preg_replace and Str_replace?

str_replace replaces a specific occurrence of a string, for instance "foo" will only match and replace that: "foo". preg_replace will do regular expression matching, for instance "/f. {2}/" will match and replace "foo", but also "fey", "fir", "fox", "f12", etc.

What is the use of Preg_match in PHP?

PHP | preg_match() Function. This function searches string for pattern, returns true if pattern exists, otherwise returns false. Usually search starts from beginning of subject string. The optional parameter offset is used to specify the position from where to start the search.

Which function is used to replace in pattern in string?

The Regex. Replace(String, String, MatchEvaluator, RegexOptions) method is useful for replacing a regular expression match if any of the following conditions is true: If the replacement string cannot readily be specified by a regular expression replacement pattern.


3 Answers

You can use a simple eval trick here:

print preg_replace('/~~(\w+)~~/e', 'array_shift($replacements)', $st);

array_shift will simply fetch the first entry from your replacement array.

Better would be using a map though ("hello" => "hi").

like image 199
mario Avatar answered Oct 11 '22 06:10

mario


I might use preg_replace_callback:

$string = '~~hello~~ there, how ~~are~~ ~~you~~?';

$pattern = '/~~(.*?)~~/'; 

var_dump(preg_replace_callback($pattern, 
    function($matches) { 
        static $replacements = array('hi', 'am', 'i'), $i = 0; 
        return $replacements[$i++ % count($replacements)]; 
    }, 
    $string));

Output:

string(19) "hi there, how am i?"
like image 27
nonopolarity Avatar answered Oct 11 '22 04:10

nonopolarity


If all you're looking to do is switch out those three specific phrases with another set of specific phrases, then you can just use str_replace as it is much faster than preg_replace.

$subject = "~~hello~~ there, how ~~are~~ ~~you~~?";
$matches = array('~~hello~~', '~~are~~', '~~you~~');
$replace = array('hi', 'am', 'i');

str_replace($matches, $replace, $subject);
like image 1
Choy Avatar answered Oct 11 '22 06:10

Choy