2013-07-12 2 views
3

EDIT : 브라우저에서 문자열을 출력하고 해석 중입니다. 어리석은 실수.PHP 빈 문자열 32

내 프로젝트에서 필요한 HTML 태그를 생성하는 클래스를 만들었습니다. php 클래스에 generateTag($control, $isCardValue = true)이라는 함수가 Card이라고합니다. 이 함수는 배열 매개 변수 $control을 통해 전달 된 속성을 기반으로 HTML 태그를 생성합니다. 나는 <select> 상자에 대한 모든 <option> 태그를 만들려면이 기능을 사용

public function generateTag($control, $isCardValue = true) { 
    if ($isCardValue) { 
     // First we convert the 'class' element to an array 
     if (isset($control['class']) && gettype($control['class']) !== 'array') { 
      $control['class'] = array($control['class']); 
     } 
     // Then we add the 'card-value' class to that array. 
     $control['class'][] = 'card-value'; 
    } 

    // The tag key is mandatory 
    $tag = '<' . $control['tag']; 
    // All keys other than 'tag' & 'content' are considered attributes for the HTML tag. 
    foreach ($control as $key => $value) { 
     switch ($key) { 
      case 'tag': 
       break; 

      case 'content': 
       break; 

      default: 
       if (gettype($value) === 'array') { 
        $tag .= ' ' . $key . '="' . implode(' ', $value) . '"'; 
       } elseif (gettype($value) === 'NULL') { 
        $tag .= ' ' . $key; 
       } else { 
        $tag .= ' ' . $key . '="' . $value . '"'; 
       } 
       break; 
     } 
    } 
    $tag .= '>'; 

    // If the 'content' key is not passed through $control, we assume that the tag 
    // doesn't need to be closed (e.g. <input> doesn't need a closing tag) 
    if (isset($control['content'])) { 
     if (gettype($control['content']) === 'array') { 
      foreach ($control['content'] as $child) { 
       $tag .= $this->generateTag($child); 
      } 
     } else { 
      $tag .= $control['content']; 
     } 
     $tag .= '</' . $control['tag'] . '>'; 
    } 

    return $tag; 
} 

: 다음과 같은 기능이 모습입니다. 태그를 생성하기 위해 배열을 반복하면됩니다.

foreach ($lists['tags'] as $key => $tag) { 
    $tag_options[$key] = array(
     'tag' => 'option', 
     'value' => $tag['tag_id'], 
     'content' => $tag['tag_name_en'], 
    ); 
    var_dump($card->generateTag($tag_options[$key], false)); 
} 

이것은 이상한 곳입니다. 내가 생성 된 문자열에 위해서 var_dump를 호출하고 나는 다음과 같은 출력을 얻을 :

string(32) "" string(35) "" string(33) "" string(33) "" string(38) "" string(32) "" string(42) "" string(30) "" string(41) "" string(34) "" string(35) "" string(34) "" string(29) "" string(36) "" string(37) "" string(31) "" string(36) "" string(67) "" string(36) "" string(33) "" string(36) "" string(36) "" 

길이의 빈 문자열 ~ 35을 만드는 있다고 나타납니다? 가장 이상한 점은 내가 substr($tag_options[$key], 0, 1)이라고 부를 때 <으로 알려줍니다. 하지만 substr($tag_options[$key], 0, 2)으로 전화하면 길이 2 인 "빈"문자열이 나옵니다. 무슨 일이 벌어지고 있는지에 대한 통찰력이 있습니까?

+0

브라우저에 HTML로 출력하고 있습니까? 'var_dump'는 HTML 인코딩을하지 않습니다. – Ryan

+0

'

+0

여기에 게시 할 코드가 너무 많습니다. 나는 문제를 보여주는 몇 줄의 문제를 줄이기 위해 재촉 할 것이다. 대부분이 일을하면서 원래 문제도 해결할 것입니다. – hek2mgl

답변

4

출력을 브라우저에서보고 있기 때문에 각 문자열의 HTML을 HTML로 파싱하고 렌더링 된 페이지에는 표시되지 않습니다. var_dump은 HTML 인코딩을 사용하지 않습니다.

알다시피 페이지의 소스에서 작동합니다. :)