Sunday 30 November 2014

PHP utf8_encode Function - Encodes an ISO-8859-1 string to UTF-8

string utf8_encode ( string $data )
Encodes an ISO-8859-1 string to UTF-8
This function encodes the string data to UTF-8, and returns the encoded version. UTF-8 is a standard mechanism used by Unicode for encoding wide character values into a byte stream. UTF-8 is transparent to plain ASCII characters, is self-synchronized (mea

Walk through nested arrays/objects and utf8 encode all strings.



<?php// Usageclass Foo {

    public
$somevar = 'whoop whoop';

}
$structure = array(
    
'object' => (object) array(
        
'entry' => 'hello wörld',
        
'another_array' => array(
            
'string',
            
1234,
            
'another string'
        
)
    ),
    
'string' => 'foo',
    
'foo_object' => new Foo);
utf8_encode_deep($structure);
// $structure is now utf8 encodedprint_r($structure);
// The functionfunction utf8_encode_deep(&$input) {
    if (
is_string($input)) {
        
$input = utf8_encode($input);
    } else if (
is_array($input)) {
        foreach (
$input as &$value) {
            
utf8_encode_deep($value);
        }
        unset(
$value);
    } else if (
is_object($input)) {
        
$vars = array_keys(get_object_vars($input));
        foreach (
$vars as $var) {
           
utf8_encode_deep($input->$var);
        }
    }
}
?>

1 7 0bbbbbbb 2 11 110bbbbb 10bbbbbb 3 16 1110bbbb 10bbbbbb 10bbbbbb 4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb

Corrigindo erros de acentuação no PHP