I'm having unexpected results when converting a namespaced object to an array.
<?php
namespace package\test;
class Test {
private $foo;
private $bar;
}
$test = new Test();
$testArray = (array) $test;
var_du开发者_开发问答mp($testArray);
and the output is
array
'�package\test\Test�foo' => null
'�package\test\Test�bar' => null
Not sure what those �
characters are from the var_dump
? I looked in the source and it appears to be �
. Basically what I need to do is trim the keys so it ends up being
array
'foo' => null
'bar' => null
but I'm not sure how to target those �
characters with a regular expression to get rid of the part that I don't want? This is for PHP 5.3.3. Thanks.
You actually don't need any regular expressions. You can do the following:
foreach ($testArray as $key => $value) {
$pos = strrpos($key, chr(0)); // find the last null character
if ($pos !== false) {
$testArray[substr($key, $pos+1)] = $value;
unset($testArray[$key]);
}
}
Every character that cannot be displayed will be replaced with a black diamond (U+FFFD REPLACEMENT CHARACTER) by the browser. So in this case it wouldn't even make sense to check for this particular substitute character.
精彩评论