简体   繁体   English

用regex php替换所有出现的内容

[英]Replace all the occurrence even with regex php

I need to replace all the occurrence of the string World [*] : with 2000. 我需要将所有出现的字符串World [*]替换为2000。

Output: 20002000hello 输出: 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)

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. []是特殊的正则表达式元字符,需要转义并在World之后的空格为0或更多匹配项。

I have this 我有这个

/(World \[.*?\] : )/

https://regex101.com/r/vbqD7a/1 https://regex101.com/r/vbqD7a/1

$str = "World [23] : World [125] : hello";
$str = preg_replace("/World (.*?) /", "2000", $str);
print $str;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM