你好我无法用美元符号替换字符串PHP str_replace函数美元符号
$string = "The $NAME brown fox jumped over the lazy dog.";
echo preg_replace('/\$NAME/', "Sample Name", $string);
输出:
The brown fox jumped over the lazy dog.
的问题是,$ NAME不与样品名称取代。 如果有任何帮助解决我的问题,我会很高兴。
你好我无法用美元符号替换字符串PHP str_replace函数美元符号
$string = "The $NAME brown fox jumped over the lazy dog.";
echo preg_replace('/\$NAME/', "Sample Name", $string);
输出:
The brown fox jumped over the lazy dog.
的问题是,$ NAME不与样品名称取代。 如果有任何帮助解决我的问题,我会很高兴。
这是因为PHP有helpfully replaced the text in the string with the contents of $NAME
你。告诉它不要那样做。
$string = 'The $NAME brown fox jumped over the lazy dog.';
特别是...使用双引号允许字符串插值,其中变量名称被解析并被替换为字符串值单引号不允许字符串插值,乐在那里提供'$ NAME'的字面值。 – Brad
啊,好的..谢谢。 – user3472449
我会将'$ NAME'改为'{NAME}'。意思是你不能无意中给它分配一个变量 – dpDesignz
既然你不是活得在做任何正则表达式搜索和替换时,我建议使用str_replace代替 – Samosa