You've successfully subscribed to Nicholas Workshop
Great! Next, complete checkout for full access to Nicholas Workshop
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

Convert Character Encoding In PHP

Nicholas Wong
Nicholas Wong

Usually while we extract information from other websites, character encoding might not correct. For example, is encoded with Shift-JIS. Therefore if your MySQL charset is UTF-8, it causes incorrect information. To handle the changing of character encoding, we can use iconv in PHP.

$url = "";  
$html = file_get_contents($url);  
$html = preg_replace("/rn|n|t/", "", $html); //remove unwanted characters  
$html = iconv("Shift_JIS","UTF-8",$html); //convert encoding  
echo $html;  

Nicholas Wong

Fullstack software engineer with strong background in computer science and extensive experience in software engineering and architecture. Studied in NYU, worked in Yahoo, Rakuten and Manulife.