note 109965 added to function.utf8-encode
| From: | OscarBroman | Date: | Thu, 06 Sep 2012 11:15:07 +0000 |
| Subject: | note 109965 added to function.utf8-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190533@lists.php.net to get a copy of this message | ||
Walk through nested arrays/objects and utf8 encode all strings.
<?php
// Usage
class 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 encoded
print_r($structure);
// The function
function 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);
}
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 83.68.230.14
----
Manual Page -- http://www.php.net/manual/en/function.utf8-encode.php
Edit -- https://master.php.net/note/edit/109965
Del: integrated -- https://master.php.net/note/delete/109965/integrated
Del: useless -- https://master.php.net/note/delete/109965/useless
Del: bad code -- https://master.php.net/note/delete/109965/bad+code
Del: spam -- https://master.php.net/note/delete/109965/spam
Del: non-english -- https://master.php.net/note/delete/109965/non-english
Del: in docs -- https://master.php.net/note/delete/109965/in+docs
Del: other reasons-- https://master.php.net/note/delete/109965
Reject -- https://master.php.net/note/reject/109965
Search -- https://master.php.net/manage/user-notes.php