简体   繁体   中英

regex curly braces pattern replace PHP

I'm having this issue cause I'm bad with regex, I would appreciate any help. I have the following case

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

Basically I want to replace {want} and {this}, I thought I could use something like:

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

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

So my output would be "This is my string and I don't want to change that"

Any help?

preg_replace uses a regular expression with delimiters and possibly modifiers. For what you are doing use str_replace :

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

使用str_replace而不是pre_replace。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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