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?
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.
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.
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.
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.
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"
).
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?"
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With