Php/MySql 在 json 中查找/替换 utf-8 字符串 [英] Php/MySql find/replace utf-8 string in json

查看:39
本文介绍了Php/MySql 在 json 中查找/替换 utf-8 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 mysql 数据库中连续有这个 json 字符串

I have this json string in a row in mysql database

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

并想找到它并用 <a> 标签替换,但为了测试,我只是选择它但看起来找不到它,我使用了这个查询:

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:

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

有什么问题?

这是原始查询:

$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."%'";

推荐答案

您需要放置转义序列来实现此目的.在 Mysql 中,涉及到第二层转义.所以用这个

You need to put escape sequence to achieve this. In Mysql, there is a second layer of escaping involved. 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;
?>

更新查询

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

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

这篇关于Php/MySql 在 json 中查找/替换 utf-8 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆