简体   繁体   English

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

[英]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: 基本上,我想替换{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);

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. preg_replace使用带有分隔符和可能的修饰符的正则表达式。 For what you are doing use str_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