php - Why doesn't this use of html_decode_entities work? -
this code adapted 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 value children's
not decoded children's
the problem has nothing html_entity_decode
, you're doing right if want decode quote entities, '
.
instead problem weren't using array_walk_recursive
correctly. in following used anonymous function , pass value reference:
function decode_data($data) { if(is_object($data) || is_array($data)){ // &$val, not $val, otherwise 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 single quote character , not entity.
Comments
Post a Comment