用regex php替换所有的出现
问题描述:
I need to replace all the occurrence of the string World [*] : with 2000.
Output: 20002000hello
How can I achieve that?
I am currently using the below code but it is not working.
preg_replace("/World [(.*?)] : /", "2000", "World [23] : World[125] : hello",-1)
我需要将所有出现的字符串World [*]替换为2000.
输出: 20002000hello strong>
如何实现?
我目前正在使用以下代码,但它无法正常工作。 p>
preg_replace(“/ World [(。*?)]:/”,“2000”,“World [23]:World [125]:hello”, - 1)
code> pre>
div>
答
You can use:
$out = preg_replace('/World\s*\[.*?\] : /', "2000", "World [23] : World[125] : hello");
//=> 20002000hello
[
and ]
are special regex meta characters that need to escaped and make space after World
optional with 0 or more matches.
答
$str = "World [23] : World [125] : hello";
$str = preg_replace("/World (.*?) /", "2000", $str);
print $str;