开发者

PHP apostophe not being escaped. Character encoding?

I have a string that has an apostrophe character that can't be escaped or searched for.

Essentially I have a string that is being pulled from a 3rd party service. It is enc开发者_JAVA技巧oded as ISO-8859-1 and when it comes in, I can't escape a particular character. A sample string is below

We’re proud to offer some exciting products that come with a full manufacturer's warranty

In this case, using mysql_real_escape_string won't escape the first apostrophe, but it will escape the others. No matter what I do, I can't seem to escape the string or replace it with a normal apostrophe.

Here is what I use to acquire the file.

$opts = array(
          'http'=>array(
            'method'=>"GET",
            'header'=>"Content-Type: text/html; charset=ISO-8859-1" 
            )
        );
        $context = stream_context_create($opts); 
        $html = file_get_contents($url, false, $context);

If I try an convert the string to UTF-8 using mb_convert_encoding(), it doesn't alter it in any way. I tried to simply replace the string using str_ireplace() on the ISO-8859-1 string and mb_ereg_replace on the UTF-8 string. Nothing has any effect.


The first apostrophe is actually the character RIGHT SINGLE QUOTATION MARK (U+2019, ) and not U+0027 ('). And that is not escaped by mysql_real_escape_string.

But besides that, U+2019 is not contained in the ISO 8859-1 character set and hence cannot be encoded with ISO 8859-1.


str_ireplace() is not safe on UTF-8 strings. A great deal of info on UTF-8 is available at this link.

If you really want to see it treated as a regular apostrophe, str_replace will work fine (as noted in the UTF-8 info page referenced):

<?php

   $str = "We’re proud to offer some exciting products that come with a full manufacturer's warranty";
   print mysql_real_escape_string(str_replace('’', "'", $str));

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜