As you are replacing all with the same, you could do either pass an array
$content = preg_replace(array($pattern1,$pattern2, $pattern3), '', $content);
or create one expression:
$content = preg_replace('/regexp1|regexp2|regexp3/', '', $content);
If the "expressions" are actually pure character strings, use str_replace instead.
As you are replacing all with the same, you could do either pass an array
$content = preg_replace(array($pattern1,$pattern2, $pattern3), '', $content);
or create one expression:
$content = preg_replace('/regexp1|regexp2|regexp3/', '', $content);
If the "expressions" are actually pure character strings, use str_replace instead.
A very readable approach is to make an array with patterns and replacements, and then use array_keys and array_values in the preg_replace
$replace = [
"1" => "one",
"2" => "two",
"3" => "three"
];
$content = preg_replace( array_keys( $replace ), array_values( $replace ), $content );
This even works with more complex patterns. The following code will replace 1, 2 and 3, and it will remove double spaces.
$replace = [
"1" => "one",
"2" => "two",
"3" => "three",
"/ {2,}/" => " "
];
$content = preg_replace( array_keys( $replace ), array_values( $replace ), $content );
I think you will need to incorporate word boundaries with a regex-based function.
Consider this strtr() demo:
$string="Rah rah, sis boom bah, I read a book on budweiser";
$p1 = array('sis','boom','bah');
$r1 = 'cheers';
$p2 = array('boo','hiss');
$r2 = 'jeers' ;
$p3 = array('guinness','heineken','budweiser');
$r3 = 'beers';
$replacements=array_merge(
array_combine($p1,array_fill(0,sizeof(
r1)),
array_combine($p2,array_fill(0,sizeof(
r2)),
array_combine($p3,array_fill(0,sizeof(
r3))
);
echo strtr($string,$replacements);
Output:
Rah rah, cheers cheers cheers, I read a jeersk on beers
// ^^^^^ Oops
You will just need to implode your needle elements using pipes and wrap them in a non-capturing group so that the word boundaries apply to all substrings, like this:
Code: (Demo)
$string="Rah rah, sis boom bah, I read a book on budweiser";
$p1 = ['sis','boom','bah'];
$r1 = 'cheers';
$p2 = ['boo','hiss'];
$r2 = 'jeers' ;
$p3 = ['guinness','heineken','budweiser'];
$r3 = 'beers';
$find=['/\b(?:'.implode('|',
p2).')\b/','/\b(?:'.implode('|',
swap=[
r2,$r3];
var_export($find);
echo "\n";
var_export($swap);
echo "\n";
echo preg_replace($find,$swap,$string);
Output:
array (
0 => '/\\b(?:sis|boom|bah)\\b/', // unescaped: /\b(?:sis|boom|bah)\b/
1 => '/\\b(?:boo|hiss)\\b/', // unescaped: /\b(?:boo|hiss)\b/
2 => '/\\b(?:guinness|heineken|budweiser)\\b/', // unescaped: /\b(?:guinness|heineken|budweiser)\b/
)
array (
0 => 'cheers',
1 => 'jeers',
2 => 'beers',
)
Rah rah, cheers cheers cheers, I read a book on beers
*Notes:
The word boundaries \b ensure that whole words are match, avoiding unintended mismatches.
If you need case-insensitivity, just use the i flag at the end of each regex pattern. e.g. /\b(?:sis|boom|bah)\b/i
$subject = 'sis + boo + guinness';
echo preg_replace(['/sis|boom|bah/','/boo|hiss/','/guinness|heineken|budweiser/'],['cheers','jeers','beers'],$subject);
// the result would be cheers + jeers + beers
replacement The string or an array with strings to replace. If this parameter is a string and the pattern parameter is an array, all patterns will be replaced by that string. If both pattern and replacement parameters are arrays, each pattern will be replaced by the replacement counterpart. If there are fewer elements in the replacement array than in the pattern array, any extra patterns will be replaced by an empty string. ...
If subject is an array, then the search and replace is performed on every entry of subject, and the return value is an array as well.
Use a callback, you can detect which pattern matched by using capturing groups, like (?:(patternt1)|(pattern2)|(etc), only the matching patterns capturing group(s) will be defined.
The only problem with that is that your current capturing groups would be shifted. To fix (read workaround) that you could use named groups. (A branch reset (?|(foo)|(bar)) would work (if supported in your version), but then you'd have to detect which pattern has matched using some other way.)
Example
function replace_callback($matches){
if(isset($matches["m1"])){
return "foo";
}
if(isset($matches["m2"])){
return "bar";
}
if(isset($matches["m3"])){
return "baz";
}
return "something is wrong ;)";
}
$re = "/(?|(?:regex1)(?<m1>)|(?:reg(\\s*)ex|2)(?<m2>)|(?:(back refs) work as intended \\1)(?<m3>))/";
$rep_string = preg_replace_callback($re, "replace_callback", $string);
Not tested (don't have PHP here), but something like this could work.
It seems to me that preg_replace_callback is the most direct solution. You just specify the alternate patterns with the | operators and inside the callback you code an if or switch. Seems the right way to me. Why did you discard it?
An alternative solution is to make a temporary replace to a special string. Say:
// first pass
$subject = preg_replace($pat0, 'XXX_MYPATTERN0_ZZZ', $subject);
$subject = preg_replace($pat1, 'XXX_MYPATTERN1_ZZZ', $subject);
$subject = preg_replace($pat2, 'XXX_MYPATTERN2_ZZZ', $subject);
// second pass
$subject = preg_replace("XXX_MYPATTERN0_ZZZ",$rep0 , $subject);
$subject = preg_replace("XXX_MYPATTERN1_ZZZ",$rep1 , $subject);
$subject = preg_replace("XXX_MYPATTERN2_ZZZ",$rep2 , $subject);
This is very ugly, does not adapt well to dynamic replacements, and it's not foolproof, but for some "run once" script it might be acceptable.
It looks like you're trying to generate a string that can be used as a URL.
There a numerous of scenarios that can happen when a user adds a title that you want to convert to a URL safe string. Someone could for instance use this:
Mess'd up --text-- just (to) stress /test/ ?our! `little` \\clean\\
url fun.ction!?-->");
Should return:
messd-up-text-just-to-stress-test-our-little-clean-url-function
Is your code ready for that? In that case you can use this function:
setlocale(LC_ALL, 'en_US.UTF8');
function toAscii(
replace=array(), $delimiter='-') {
if( !empty($replace) ) {
$str = str_replace((array)$replace, ' ', $str);
}
$clean = iconv('UTF-8', 'ASCII//TRANSLIT',
clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
return $clean;
}
You can use array in pattern and replacement text. For example
$pattern=array();
$pattern[0]="/\\s/";
$pattern[1]="/-\/-/";
$replace=array();
$replace[0]="-";
$replace[1]="-";
preg_replace($pattern, $replace, $item_replace_en[0]);
You can use as many patterns and replacement you want. For more info, refer to http://php.net/manual/en/function.preg-replace.php
preg_replace can take an array just like str_replace
$string = 'I have a match1 and a match3, and here\'s a match2';
$find = ['/match1/', '/match2/'];
$replace = ['foo', 'bar'];
$result = preg_replace($find, $replace, $string);
You can also use T-Regx library
<?php
$stylesheet = file_get_contents('temp/'.$user.'/css/mobile.css');
$cssTag = 'bodybg';
$stylesheet = pattern("(/\*" . $cssTag . "\*/).*?(/\*/" . $cssTag . "\*/)", 'i')
->replace($stylesheet)
->all()
->by()
->map([
'searchforme' => 'replacewithme',
'searchforme1' => 'replacewithme2',
'searchforme1' => 'replacewithme2',
]);
You don't need delimiters /, because T-Regx automatically adds them