简体   繁体   English

Php/MySql 在 json 中查找/替换 utf-8 字符串

[英]Php/MySql find/replace utf-8 string in json

I have this json string in a row in mysql database我在 mysql 数据库中连续有这个 json 字符串

{"editor":"<p>\u067e\u062f\u0631\u0627\u0645<\/p>"}

And want to find this and replace with <a> tag, but for test, I just select it but look like it can't find it, I used this query:并想找到它并用<a>标签替换,但为了测试,我只是选择它但看起来找不到它,我使用了这个查询:

SELECT * FROM `wp_postmeta` WHERE `meta_value` LIKE '%\u067e\u062f\u0631\u0627\u0645%'

What is the problem?问题是什么?

This is original query:这是原始查询:

$keyword_j = "\u067e\u062f\u0631\u0627\u0645";

$sql2 = "UPDATE `wp_postmeta` SET `meta_value` = REPLACE(meta_value, '".$keyword_j."', '<a href=\"\/".$link."\">".$keyword_j."<\/a>') WHERE `meta_value` LIKE '%".$keyword_j."%'";

You need to put escape sequence to achieve this.您需要放置转义序列来实现此目的。 In Mysql, there is a second layer of escaping involved.在 Mysql 中,涉及到第二层转义。 So use this所以用这个

SELECT * FROM `wp_postmeta` WHERE `meta_value` LIKE '%\\\\u067e\\\\u062f\\\\u0631\\\\u0627\\\\u0645%'

To automatically search and replace slash, use this要自动搜索和替换斜杠,请使用此

<?php
$keyword_j = "\u067e\u062f\u0631\u0627\u0645";
$x = str_replace("\\", "\\\\\\\\", $keyword_j);
echo $x;
?>

Update Query更新查询

$str = addslashes(htmlentities("<a href='".$link."'>".$keyword_j."</a>"));

$sql2 = "UPDATE wp_postmeta SET meta_value ='".$str."' WHERE meta_value LIKE '%".$x."%'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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