一个撇号呈现为¡什么PHP函数将它显示为'? something_Decode? [英] An apostrophe is rendering as â€tm. What PHP function will display it as ' ? something_Decode?

查看:132
本文介绍了一个撇号呈现为¡什么PHP函数将它显示为'? something_Decode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在抓取一些推文,并将它们打印在我的网站上,卷曲的撇号将被渲染为“m。不是很好。什么php函数应该运行字符串通过得到这些奇怪的字符显示为更接近'?

解决方案

使用以下函数:

  function htmlallentities($ str){
$ res ='';
$ strlen = strlen($ str);
for($ i = 0; $ i< $ strlen; $ i ++){
$ byte = ord($ str [$ i]);
if($ byte< 128)// 1-byte char
$ res。= $ str [$ i];
elseif($ byte< 192); // invalid utf8
elseif($ byte< 224)// 2-byte char
$ res。='&#'((63& $ byte)* 64 +(63& ord ($ str [++ $ i])))。';';
elseif($ byte <240)// 3-byte char
$ res。='&#'。((15& $ byte)* 4096 +(63& ord ++ $ i]))* 64 +(63& ord($ str [++ $ i])))。
elseif($ byte< 248)// 4-byte char
$ res。='&#'。((15& $ byte)* 262144 +(63& ord ++ $ i]))* 4096 +(63& ord($ str [++ $ i]))* 64 +(63& ord($ str [++ $ i])))。
}
return $ res;
}

调用:

  $ str = htmlallentities($ str); 

这会将utf-8-chars改成htmlentities,所以你可以用不同的编码来显示它们。 / p>

I'm grabbing some tweets and printing them out on my site and curly apostrophes are being rendered as "â€tm". This is not good. What php function should I run the string through to get these weird characters to display as something closer to '?

解决方案

You could try to use the following function:

function htmlallentities($str){
  $res = '';
  $strlen = strlen($str);
  for($i=0; $i<$strlen; $i++){
    $byte = ord($str[$i]);
    if($byte < 128) // 1-byte char
      $res .= $str[$i];
    elseif($byte < 192); // invalid utf8
    elseif($byte < 224) // 2-byte char
      $res .= '&#'.((63&$byte)*64 + (63&ord($str[++$i]))).';';
    elseif($byte < 240) // 3-byte char
      $res .= '&#'.((15&$byte)*4096 + (63&ord($str[++$i]))*64 + (63&ord($str[++$i]))).';';
    elseif($byte < 248) // 4-byte char
      $res .= '&#'.((15&$byte)*262144 + (63&ord($str[++$i]))*4096 + (63&ord($str[++$i]))*64 + (63&ord($str[++$i]))).';';
  }
  return $res;
}

call:

$str = htmlallentities($str);

this will change utf-8-chars into htmlentities, so you can display them in different encodings.

这篇关于一个撇号呈现为¡什么PHP函数将它显示为'? something_Decode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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