2015-11-19 147 views
0

替换字符串中的话,我有一个这样的字符串:搜索和正则表达式

"This is a random string with @[certain] words with this format @[something]" 

我想以取代@[]

所以我的结果必须是这样说的话包裹:

"This is a random string with words with the format" 

我正在使用PHP。

+0

欢迎SO!请显示解决问题的尝试。看[问]和[mcve] – Tushar

回答

1

您可以使用模式的正则表达式并使用preg_replace替换您的字符串。

这样的事情,

$str = "This is a random string with @[certain] words with this format @[something]"; 
$newStr = preg_replace('/\ @\[(\w+)\]/','',$str); 

您也可以在一行写这篇文章,

echo preg_replace('/\ @\[(\w+)\]/','',"This is a random string with @[certain] words with this format @[something]");