简体   繁体   English

在URL中转换UTF-8字符以在PHP中进行SQL查询

[英]Converting UTF-8 Characters in urls for SQL Query in PHP

I am running a SQL query on a Wordpress blog to a get json feed of posts. 我在Wordpress博客上运行SQL查询,以获取帖子的json供稿。 Here is my code: http://pastebin.com/jvaM9ySr 这是我的代码: http : //pastebin.com/jvaM9ySr

It works fine but some of the post url's are coming through with what I think are UTF-8 special characters like 它工作正常,但某些发布网址带有我认为的UTF-8特殊字符,例如

ranking-of-the-country%e2%80%99s-101-best-desserts

where I guess a blog editor pasted in a fancy apostrophe. 我猜是在博客编辑器中贴上了花哨的撇号。

other examples have %e2%84%a2 in the middle of the url for the trademark symbol and even %cf%83 for the greek sigma letter. 其他示例在商标符号的网址中间有%e2%84%a2,对于希腊sigma字母甚至还有%cf%83。

I'd like to convert these to show what the actual special character is in the json feed. 我想将其转换为显示json提要中实际的特殊字符。

I am using this to match the urls in the Google Analytics API and the corresponding Analytics urls contain the special character itself and not code like: 我正在使用它来匹配Google Analytics(分析)API中的网址,相应的Google Analytics (分析)网址本身包含特殊字符,而不是类似以下代码的代码:

 ranking-of-the-country’s-101-best-desserts

so these few cases aren't matching because of this special character issue. 因此由于这种特殊字符问题,这几种情况不匹配。

I've tried to use utf8_encode and utf8_decode in various places in the code that is in the pastebin with no luck. 我试过在pastebin中没有运气的代码中的各个地方使用utf8_encode和utf8_decode。

Any one have any ideas? 有人有想法么? Thanks! 谢谢!

All you need is 所有你需要的是

$text = "ranking-of-the-country%e2%80%99s-101-best-desserts";
echo 'TRANSLIT : ', iconv("UTF-8", "ISO-8859-1//TRANSLIT", $text), PHP_EOL;

I have had the similar problem like you in another programming language. 我在另一种编程语言中也遇到过类似的问题。

This is not utf8 encode but url encode . 这不是utf8 encode而是url encode

Try to use: 尝试使用:

urlencode(string)

and

urldecode(string)

In the 2 cases (with decoding all the %char change with the correct utf char). 在2种情况下(使用正确的utf char解码所有%char char时)。

Here you can find more information and examples: http://php.net/manual/en/function.urlencode.php 在这里您可以找到更多信息和示例: http : //php.net/manual/en/function.urlencode.php

UPDATE : for your code: I haven't tested, but you must or decoding the url when you store the response value here UPDATE :针对您的代码:我尚未测试,但是在此处存储响应值时必须对URL进行解码

    while($r = mysql_fetch_assoc($sth)) {
    if $r is a url
    $rows = decode($r);
    else
    $rows[] = $r;   
    }

OR differentiate the printing at the end of your code (something like the code before): doing for each variable in the array the appropriate printing (and when you "catch" the "url" you do the decoding), here: 或区分代码结尾处的打印(类似于之前的代码):对数组中的每个变量进行适当的打印(当您“捕获”“ url”时,您进行解码),这里:

print strip_tags(json_encode($rows));

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

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