使用递归将html_entity_decode应用于对象和数组


Apply html_entity_decode to objects and arrays using recursion

我正在尝试构建一个函数,将html_entity_decode应用于对象和数组。由于我事先不知道结构,而且子属性也可以是对象或数组,因此似乎可以使用简单的递归函数。我不知道为什么以下不起作用:

function decode($data){  
    if(is_object($data) || is_array($data)){
        foreach($data as &$value)
            $value = $this->decode($value);
    }
    else $data = html_entity_decode($data);
    return $data;
}

我还尝试了以下方法,但也不起作用:

function decode($data){
    if(is_object($data))
        $data = get_object_vars($data);
    $data = is_array($data) ? array_map(array('MyClassName', 'decode'), $data) : html_entity_decode($data);
    return $data;
}

这两个函数都不会对数据产生任何影响。我做错了什么?

主要问题是,如果不将object转换为array ,则尝试使用像数组is_object($data) || is_array($data)这样的对象将无法真正工作

其他问题是基于错误的变量名称和未返回正确的变量。您可以尝试

$std = new stdClass();
$std->title = array("x" => "<a>XXX</a>","y" => "<b>YYY</b>");
$std->body = "<p> THis is the Body </p>";
$var = array(
        "a" => "I'll '"walk'" the <b>dog</b> now",
        "b" => array("<b>Hello World</b>",array(array("Yes am <strong> baba </strong>"))),
        "c" => $std
);
$class = new MyClassName();
$encode = $class->encode($var); // encode 
$decode = $class->decode($encode);  // decode it back
print_r($encode);
print_r($decode);

编码阵列

Array
(
    [a] => I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt; now
    [b] => Array
        (
            [0] => &lt;b&gt;Hello World&lt;/b&gt;
            [1] => Array
                (
                    [0] => Array
                        (
                            [0] => Yes am &lt;strong&gt; baba &lt;/strong&gt;
                        )
                )
        )
    [c] => stdClass Object
        (
            [title] => Array
                (
                    [x] => &lt;a&gt;XXX&lt;/a&gt;
                    [y] => &lt;b&gt;YYY&lt;/b&gt;
                )
            [body] => &lt;p&gt; THis is the Body &lt;/p&gt;
        )
)

解码阵列

Array
(
    [a] => I'll "walk" the <b>dog</b> now
    [b] => Array
        (
            [0] => <b>Hello World</b>
            [1] => Array
                (
                    [0] => Array
                        (
                            [0] => Yes am <strong> baba </strong>
                        )
                )
        )
    [c] => stdClass Object
        (
            [title] => Array
                (
                    [x] => <a>XXX</a>
                    [y] => <b>YYY</b>
                )
            [body] => <p> THis is the Body </p>
        )
)

查看实时演示

class MyClassName {
    function encode($data) {
        if (is_array($data)) {
            return array_map(array($this,'encode'), $data);
        }
        if (is_object($data)) {
            $tmp = clone $data; // avoid modifing original object
            foreach ( $data as $k => $var )
                $tmp->{$k} = $this->encode($var);
            return $tmp;
        }
        return htmlentities($data);
    }
    function decode($data) {
        if (is_array($data)) {
            return array_map(array($this,'decode'), $data);
        }
        if (is_object($data)) {
            $tmp = clone $data; // avoid modifing original object
            foreach ( $data as $k => $var )
                $tmp->{$k} = $this->decode($var);
            return $tmp;
        }
        return html_entity_decode($data);
    }
}

您到底想做什么:用html实体解码的值替换现有值,或者用编码的值复制整个数据结构?

如果你想替换,那么你应该通过引用传递函数参数:

function decode( &$data ){

如果你想复制——它应该按原样工作(或者请解释你所说的"以下内容不起作用"到底是什么意思)。

数组遍历递归:http://php.net/manual/en/function.array-walk-recursive.php也许是这样的:

function decode($data){  
    $data = html_entity_decode($data);
}
function decode_data($data){
    if(is_object($data) || is_array($data)){
       array_walk_recursive($data, 'decode');
    }else{
       $data = html_entity_decode($data);
    }
    return $data;
}