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

Saturday, 29 November 2014

com_create_guid Generate a globally unique identifier

string com_create_guid ( void )
Generate a globally unique identifier (GUID)
Generates a Globally Unique Identifier (GUID).

The phunction PHP framework (http://sourceforge.net/projects/phunction/) uses the following function to generate valid version 4 UUIDs:



<?phpfunction GUID()
{
    if (
function_exists('com_create_guid') === true)
    {
        return
trim(com_create_guid(), '{}');
    }
    return
sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));

}
?>

The output generated by the sprintf() and mt_rand() calls is identical to com_create_guid() results.