preg_replace 每次替换有多个模式的数组
preg_replace arrays with multiple patterns per replacement
我不认为我可以一次做到这一点 swell foop,但我不禁要问 :-)
我当然可以定义一对一的$pattern
和$replacement
数组,我可以重复定义$pattern
数组和$replacement
字符串,但是我可以吗两者同时进行?
假设我有如下三组:
$p1 = array('sis','boom','bah');
$r1 = 'cheers';
$p2 = array('boo','hiss');
$r2 = 'jeers';
$p3 = array('guinness','heineken','budweiser');
$r3 = 'beers';
并且我想在一个大的干草堆中用它们各自的单一替换替换所有模式实例。我可以定义一个 preg_replace
调用来完成此操作吗?
$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.
我认为您需要将单词边界与基于正则表达式的函数结合起来。
考虑这个 strtr()
演示:
$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($p1),$r1)),
array_combine($p2,array_fill(0,sizeof($p2),$r2)),
array_combine($p3,array_fill(0,sizeof($p3),$r3))
);
echo strtr($string,$replacements);
输出:
Rah rah, cheers cheers cheers, I read a jeersk on beers
// ^^^^^ Oops
您只需要使用管道将 needle
元素内爆并将它们包装在非捕获组中,以便词边界适用于所有子字符串,如下所示:
代码:(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('|',$p1).')\b/','/\b(?:'.implode('|',$p2).')\b/','/\b(?:'.implode('|',$p3).')\b/'];
$swap=[$r1,$r2,$r3];
var_export($find);
echo "\n";
var_export($swap);
echo "\n";
echo preg_replace($find,$swap,$string);
输出:
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
*备注:
单词边界 \b
确保整个单词匹配,避免意外的不匹配。
如果您需要不区分大小写,只需在每个正则表达式模式的末尾使用 i
标志。例如/\b(?:sis|boom|bah)\b/i
我不认为我可以一次做到这一点 swell foop,但我不禁要问 :-)
我当然可以定义一对一的$pattern
和$replacement
数组,我可以重复定义$pattern
数组和$replacement
字符串,但是我可以吗两者同时进行?
假设我有如下三组:
$p1 = array('sis','boom','bah');
$r1 = 'cheers';
$p2 = array('boo','hiss');
$r2 = 'jeers';
$p3 = array('guinness','heineken','budweiser');
$r3 = 'beers';
并且我想在一个大的干草堆中用它们各自的单一替换替换所有模式实例。我可以定义一个 preg_replace
调用来完成此操作吗?
$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.
我认为您需要将单词边界与基于正则表达式的函数结合起来。
考虑这个 strtr()
演示:
$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($p1),$r1)),
array_combine($p2,array_fill(0,sizeof($p2),$r2)),
array_combine($p3,array_fill(0,sizeof($p3),$r3))
);
echo strtr($string,$replacements);
输出:
Rah rah, cheers cheers cheers, I read a jeersk on beers
// ^^^^^ Oops
您只需要使用管道将 needle
元素内爆并将它们包装在非捕获组中,以便词边界适用于所有子字符串,如下所示:
代码:(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('|',$p1).')\b/','/\b(?:'.implode('|',$p2).')\b/','/\b(?:'.implode('|',$p3).')\b/'];
$swap=[$r1,$r2,$r3];
var_export($find);
echo "\n";
var_export($swap);
echo "\n";
echo preg_replace($find,$swap,$string);
输出:
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
*备注:
单词边界 \b
确保整个单词匹配,避免意外的不匹配。
如果您需要不区分大小写,只需在每个正则表达式模式的末尾使用 i
标志。例如/\b(?:sis|boom|bah)\b/i