2013-02-13 2 views
1

객체 및 배열에 html_entity_decode를 적용 할 함수를 작성하려고합니다. 미리 구조체를 알지 못하고 자식 속성도 객체 나 배열이 될 수 있기 때문에 간단한 재귀 함수가 길을가는 것처럼 보였습니다. 다음 작동하지 않는 이유를 알아낼 수 없어요 :재귀를 사용하여 객체 및 배열에 html_entity_decode 적용

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; 
} 

가 어느 함수가 데이터에 영향을 미치지 : 나는 또한 중 하나가 작동하지 않습니다 다음을 시도

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; 
} 

. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

답변

9

주요 문제는 당신이 변환 정말 제외하고는 작동하지 않을 것입니다 배열 is_object($data) || is_array($data) 같은 개체에 작업을 시도하고 있다는 점이다 objectarray

다른 문제는 당신이 시도 할 수 있습니다 잘못된 변수 이름에베이스와 적절한 변수를 반환하지 않습니다

,895,

인코딩 된 배열

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> 
     ) 

) 

디코딩 된 배열

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> 
     ) 

) 

See Live Demo

정확히 원하는 작업
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); 
    } 
} 
+1

을 제거하면 첫 번째 기능이 작동한다고 생각합니다! 나는 그것을 가로 질러 왔고, 그것을 나의 자신의 프로젝트에서 사용하기로 결정했다. 고맙습니다! –

+0

@TyBailey http://stackoverflow.com/a/15034807/1226894 또한 'htmlentities'가 아닌 다른 필터를 사용합니다. – Baba

+0

감사합니다. 이것은 잘 작동합니다. 우연히도, 내 원래의 기능 모두 (적어도 내 목적을 위해). 문제는 내 jquery 아약스 요청과 것 같습니다. PHP 스크립트가 디코드 된 응답을 보내고 있지만 어떻게 든 엔티티 인코딩입니다. 물론 원래 질문에 대해 100 % 정확하므로 답변을 수락합니다. 다시 한번 감사드립니다. –

0

무엇 배열 워크 재귀에 대한 : 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; 
} 
+1

또한 "$ this->" –

1

: HTML을 entit 기존 값을 대체 y 디코딩 된 값을 사용하거나 전체 데이터 구조의 복사본을 인코딩 된 값으로 만드시겠습니까?

당신이 다음 교체 할 경우 참조에 의해 함수 인수를 전달해야 : 복사 확인하려면

function decode(&$data){ 

은 - 그것이있는 방식으로 작동한다을 (또는 정확히 무슨 뜻 이죠 설명해주십시오 "다음은 작동하지 않음").

관련 문제