0

This code is adapted from the PHP manual entry on html_entity_decode()

protected function decode($data)
{
    $data = html_entity_decode($data, ENT_QUOTES,'UTF-8');
    //echo $data;
    return $data;
}
protected function decode_data($data)
{

    if(is_object($data) || is_array($data)){
        array_walk_recursive($data,array($this,'decode'));
    }else{
        $data = html_entity_decode($data, ENT_QUOTES,'UTF-8');
    }
    return $data;
}

If data contains an a value like Children's it does not get decoded to Children's

0

1 Answer 1

1

The problem has nothing to do with html_entity_decode, you're doing that right if you want to decode just quote entities, even those like '.

Instead your problem is that you weren't using array_walk_recursive correctly. In the following I used an anonymous function and pass the value as a reference:

function decode_data($data)
{
    if(is_object($data) || is_array($data)){
        // &$val, not $val, otherwise the array value wouldn't update.
        array_walk_recursive($data, function(&$val, $index) {
            $val = html_entity_decode($val, ENT_QUOTES,'UTF-8');
        });
    }else{
        $data = html_entity_decode($data, ENT_QUOTES,'UTF-8');
    }
    return $data;
}
$array = [
     "Children's",
     "Children's",
];
print_r( decode_data($array) );

Outputs both Children's as a single quote character and not as entity.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this sorted it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.