繁体   English   中英

正则表达式花括号模式替换PHP

[英]regex curly braces pattern replace PHP

我遇到此问题是因为我对正则表达式不好,我将不胜感激。 我有以下情况

 $text = "This is my string and I {want} to change {this}";

基本上,我想替换{want}和{this},我认为我可以使用类似以下内容的东西:

 $patterns = array();
 $patterns[0] = "{want}";
 $patterns[1] = "{this}"; 
 $replacement = array();
 $replacement[0] = "don't";
 $replacement[1] = "that";

 $new = preg_replace($pattern, $replacement, $text);

所以我的输出是“这是我的字符串,我不想更改它”

有什么帮助吗?

preg_replace使用带有分隔符和可能的修饰符的正则表达式。 对于您正在做什么,请使用str_replace

$new = str_replace($patterns, $replacement, $text);

使用str_replace而不是pre_replace。

$new = str_replace($patterns, $replacement, $text);

暂无
暂无

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

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