[英]preg_replace not replacing with ajax
我有以下代码
$text = '["{!Account__!http://localhost/MF/Public/__NotActivated}","email"]';
$replcmnt = array(
'#{!Account__!http:\/\/localhost\/MF/Public\/__NotActivated}#' => 'text to replace'
);
$text = preg_replace(array_keys($replcmnt),$replcmnt,$text);
我需要输出为'["text to replace","email"]'
但是由于某种原因替换未替换。 我觉得这很奇怪,因为这在我加载页面时有效,但是如果我通过ajax请求运行它就无法正常工作
我还必须注意,如果我从下划线之间的内部部分删除了任何斜线,则会进行替换。 因此,问题必定是那些斜线。
例如
$replcmnt = array(
'#{!Account__!http:-localhost-MF-Public-__NotActivated}#' => 'text to replace'
);
我想,由于缺少斜线,以上内容将被替换。
任何帮助是极大的赞赏
编辑 :
这是替换之前的数据:
$text
["{!Account__!http:\/\/localhost\/MF\/Public\/__NotActivated}","email"]
$replcmnt
Array
(
[#{!Account__!http:\/\/localhost\/MF\/Public\/__NotActivated}#] => some long text
)
使用str_replace()
而不是preg_replace()
,因为您没有进行任何模式匹配。
我建议不要使用JSON作为字符串,而应该使用它所代表的实际数据结构。 您可以反序列化JSON,只需替换数组元素,如下所示:
$test_array = json_decode($text);
$test_array[0] = $text_to_replace;
$new_json = json_encode($test_array);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.