php preg_replace 不执行任何操作
由于某种原因,我的 preg_replace 调用不起作用,我已经检查了我能想到的所有内容,但无济于事。有什么建议吗?
foreach ($this->vars as $key=>$var)
{
preg_replace("/\{$key\}/", $var, $this->tempContentXML);
}
vars 是一个数组,其中包含字符串中需要替换的 $key->value,tempContentXML 是一个包含 XML 数据的字符串。
字符串
...<table:table-cell table:style-name="Table3.B1" office:value-type="string"><text:p text:style-name="P9">{Reference}</text:p></table:table-cell></table:table-row><table:table-row table:style-name="Table3.1"><...
EX 的一部分。
$this->vars['Reference'] = Test;
foreach ($this->vars as $key=>$var)
{
preg_replace("/\{$key\}/", $var, $this->tempContentXML);
}
这应该用 $key 处数组中的值替换字符串 {Reference}
但它不起作用。
For some reason my preg_replace call is not working, I have check everything I can think of to no avail. Any suggestions?
foreach ($this->vars as $key=>$var)
{
preg_replace("/\{$key\}/", $var, $this->tempContentXML);
}
vars is an array containing the $key->value that needs to be replaced in the string, tempContentXML is a string containing XML data.
Piece of the string
...<table:table-cell table:style-name="Table3.B1" office:value-type="string"><text:p text:style-name="P9">{Reference}</text:p></table:table-cell></table:table-row><table:table-row table:style-name="Table3.1"><...
EX.
$this->vars['Reference'] = Test;
foreach ($this->vars as $key=>$var)
{
preg_replace("/\{$key\}/", $var, $this->tempContentXML);
}
That should replace the string {Reference} with the value in the array at $key
But it is not working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
替换不会就地发生(返回新字符串)。
除此之外,不要使用正则表达式进行纯字符串替换(假设
$this->vars
不包含正则表达式):The replacement does not happen in-place (the new string returned).
Besides that, don't use a regex for plain string replacements ever (assuming
$this->vars
does not contain regexes):