Preg_replace:“foo”,“bar”,“foo bar”和“bar foo”

时间:2021-12-19 22:28:43

I use a regular expression to find some words, and do something with the words/strings.

我使用正则表达式来查找单词,并使用单词/字符串执行某些操作。

My example :

我的例子:

I wan't to set <strong> tag with all words I find :

我不想用我发现的所有单词设置标签:

$string = 'Hello, there is foo, after there is bar but now I need foo bar.'

$html = preg_replace('/(foo|bar)/', '<strong>$1</strong>', $string);

$html will be 'Hello, there is <strong>foo</strong>, after there is <strong>bar</strong> but now I need <strong>foo</strong> <strong>bar</strong>.'

And I wan't, if they are 1 word following 1 other word searched, that result :

而且,如果他们在搜索到另外一个单词之后是1个单词,那么结果是:

'Hello, there is <strong>foo</strong>, after there is <strong>bar</strong> but now I need <strong>foo bar</strong>.'

How can I modify my regular expression to get the 2 words between us and work on it without separated tag ?

如何修改我的正则表达式以获取我们之间的2个单词并在没有分隔标记的情况下处理它?

Thanks

谢谢

3 个解决方案

#1


2  

$search = 'foo bar blah';
$string = 'Hello, there is foo, after there is bar but now I need foo bar blah.';

$search = preg_quote($search);
$regex = $search . '|' . str_replace(' ', '|', $search);

$html = preg_replace('/\b(' . $regex . ')\b/', '<strong>$1</strong>', $string);

echo $html; // Outputs: Hello, there is <strong>foo</strong>, after there is <strong>bar</strong> but now I need <strong>foo bar blah</strong>.

#2


1  

Here we are:

我们到了:

$html = preg_replace('/(foo|bar)( (foo|bar))*/', '<strong>$0</strong>', $string);

Less readable but slightly more efficient (non-capturing groups):

可读性较差但效率稍高(非捕获组):

$html = preg_replace('/(?:foo|bar)(?: (?:foo|bar))*/', '<strong>$0</strong>', $string);

Maybe there's a solution to not repeat (foo|bar)...

也许有一个不重复的解决方案(foo | bar)......

Ha, and don't forget the \b's or like, if you don't want to match in "arfooo" ;-)

哈,如果你不想在“arfooo”中匹配,不要忘记\ b或者喜欢;-)


edit: if you need something more dynamic, thanks to cryptic ツ for the idea:

编辑:如果你需要更有活力的东西,感谢神秘的ツ:

$words = array('foo', 'bar');

// require PHP 5.3, not very efficient code
$escaped_words = array_map(function ($word) {
    return preg_quote($word, '/');
}, $words);

$pattern = '(?:' . implode('|', $escaped_words) . ')';

$html = preg_replace('/'.$pattern.'(?: '.$pattern.')*/', '<strong>$0</strong>', $string);

#3


0  

$string = 'Hello, there is foo, after there is bar but now I need foo bar.';
$string.= ' Lets throw a bar foo in there as well!';

$html = preg_replace('/(bar foo|foo bar|foo|bar)/', '<strong>$1</strong>', $string);

echo $html;

#1


2  

$search = 'foo bar blah';
$string = 'Hello, there is foo, after there is bar but now I need foo bar blah.';

$search = preg_quote($search);
$regex = $search . '|' . str_replace(' ', '|', $search);

$html = preg_replace('/\b(' . $regex . ')\b/', '<strong>$1</strong>', $string);

echo $html; // Outputs: Hello, there is <strong>foo</strong>, after there is <strong>bar</strong> but now I need <strong>foo bar blah</strong>.

#2


1  

Here we are:

我们到了:

$html = preg_replace('/(foo|bar)( (foo|bar))*/', '<strong>$0</strong>', $string);

Less readable but slightly more efficient (non-capturing groups):

可读性较差但效率稍高(非捕获组):

$html = preg_replace('/(?:foo|bar)(?: (?:foo|bar))*/', '<strong>$0</strong>', $string);

Maybe there's a solution to not repeat (foo|bar)...

也许有一个不重复的解决方案(foo | bar)......

Ha, and don't forget the \b's or like, if you don't want to match in "arfooo" ;-)

哈,如果你不想在“arfooo”中匹配,不要忘记\ b或者喜欢;-)


edit: if you need something more dynamic, thanks to cryptic ツ for the idea:

编辑:如果你需要更有活力的东西,感谢神秘的ツ:

$words = array('foo', 'bar');

// require PHP 5.3, not very efficient code
$escaped_words = array_map(function ($word) {
    return preg_quote($word, '/');
}, $words);

$pattern = '(?:' . implode('|', $escaped_words) . ')';

$html = preg_replace('/'.$pattern.'(?: '.$pattern.')*/', '<strong>$0</strong>', $string);

#3


0  

$string = 'Hello, there is foo, after there is bar but now I need foo bar.';
$string.= ' Lets throw a bar foo in there as well!';

$html = preg_replace('/(bar foo|foo bar|foo|bar)/', '<strong>$1</strong>', $string);

echo $html;