Search code examples
phphtmlcharacter-encoding

An apostrophe is rendering as â€tm. What PHP function will display it as ' ? something_Decode?


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 '?


Solution

  • 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.