preg 替换查找关键字
非常简单的预浸料替换,但我想不通。
我需要搜索关键字,例如:{this_is_a_key}
并将其替换为其他内容。
$text = 'this is a sentence with a {this_is_a_key} in it';
echo preg_replace('/\{\w{1}\}/i','keyword',$text);
//output should be
//## "this is a sentence with a keyword in it" ##//
到目前为止,这对我不起作用,我尝试了 ()
和 []
的组合,但没有运气
very simple preg replace but I can't think right.
I need to search for keywords such as: {this_is_a_key}
and replace it with something else.
$text = 'this is a sentence with a {this_is_a_key} in it';
echo preg_replace('/\{\w{1}\}/i','keyword',$text);
//output should be
//## "this is a sentence with a keyword in it" ##//
so far this doesn't work for me and I have tried a combination of ()
and []
but no luck
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
\w{1}
表示一个“单词字符”。请尝试使用\w+
或[^\}]+
代替。此外,当使用
\w
时,不需要i
修饰符。\w{1}
means one "word character". Try\w+
or[^\}]+
instead.Also, when using
\w
, there's no need for thei
modifier.