Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fix characters in PHP

Anyone know what this is and how to fix it? “The OMG�

<p>After 32 years in its former location, this popular restaurant 
   and bar moved to the bottom of the Avalon Bay Luxury residential building 
   that's just a walk from Angel Stadium. Modern and welcoming, the expansive 
   space is where fans, locals and families gather for upscale twists on classic
   American dishes. Burgers here have a decidedly fun flair like “The OMG� 
   that is a burger surely meant for sharing (with many)--it's so huge that it's 
   served on a 14-inch bun. The restaurant also specializes in seafood with such 
   items as Chilean sea bass, mahi mahi and swordfish. The lounge also serves 
   signature drinks like  the Rally Monkey Martini in tribute to the mascot 
   of the Angels.</p>

This seems to a problem because of a function I have to run on the JSON data before decoding it or JSON decoding will fail:

function safeJSON_chars($data) {
$aux = str_split($data); 
foreach($aux as $a) { 
    $a1 = urlencode($a); 
    $aa = explode("%", $a1); 
    foreach($aa as $v) { 
        if($v!="") { 
            if(hexdec($v)>127) { 
            $data = str_replace($a,"&#".hexdec($v).";",$data); 
            } 
        } 
    } 
} 
return $data;}
like image 216
MrPHP Avatar asked Sep 03 '26 19:09

MrPHP


2 Answers

Have a look at this page

Unicode-friendly PHP and MySQL

On this page you will find an easy and clear explaination of UTF-8 encoding and how to apply this in your websites along with some practical examples.

You will also need to make sure that your files are saved using UTF-8 encoding (without BOM).

like image 57
Michiel Pater Avatar answered Sep 05 '26 09:09

Michiel Pater


In PHP, you can set the page charset:

header("Content-type: text/html; charset=utf-8");
like image 43
Michael Berkowski Avatar answered Sep 05 '26 08:09

Michael Berkowski