Programming Tutorials

fixEncoding in PHP

By: mrezair in PHP Tutorials on 2011-07-31  

I found this little function very useful in fixing strings that are not in utf-8 but need be converted .

<?php 
// Fixes the encoding to uf8 
function fixEncoding($in_str) 
{ 
  $cur_encoding = mb_detect_encoding($in_str) ; 
  if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8")) 
    return $in_str; 
  else 
    return utf8_encode($in_str); 
} // fixEncoding 
?>






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in PHP )

PHP code to write to a CSV file from MySQL query

Different versions of PHP - History and evolution of PHP

PHP code to import from CSV file to MySQL

Encrypting files using GnuPG (GPG) via PHP

PHP Warning: Unknown(): Unable to load dynamic library '/usr/local/php4/lib/php/extensions/no-debug ......

Send push notifications using Expo tokens in PHP

Decrypting files using GnuPG (GPG) via PHP

A Basic Example using PHP in AWS (Amazon Web Services)

Count occurrences of a character in a String in PHP

Error: Length parameter must be greater than 0

Password must include both numeric and alphabetic characters - Magento

Reading word by word from a file in PHP

Parent: child process exited with status 3221225477 -- Restarting

Convert a hex string into a 32-bit IEEE 754 float number in PHP

PHP file upload prompts authentication for anonymous users

Latest Articles (in PHP)