2011-10-13 2 views
2

나는 이해할 수 없다 HTML로,String to HTML? 아래의 코드는 문자열로 문자열을 반환하지만 왜

function en_code($string) 
{ 
    # find the match of [br] = a break = <br> 
    $output = preg_replace('/\[(?:&nbsp;|\s)*([br]+)(?:&nbsp;|\s)*\]/', '&lt;$1 /&gt;', $string); 

    # return the result 
    return $output; 
} 

$string = 'Wallace and Gromit\'s Children Foundation the whole campaign was exemplary, showing true professionalism, creativity and an amazing understanding of what makes a strong news story.\'[br][br]Wallace & Gromit\'s Children\'s Foundation'; 

echo en_code($string); 

반환

Wallace and Gromit's Children Foundation the whole campaign was 
exemplary, showing true professionalism, creativity and an amazing 
understanding of what makes a strong news story.'<br /><br />Wallace & 
Gromit's Children's Foundation 

하지만이 같은 반환해야합니다,

Wallace and Gromit 's Children Foundation 전체 캠페인은 참된 전문성과 창의력 그리고 놀랄만 한 모습을 보여주는 의 모범이되었습니다. understandi 강력한 뉴스 스토리를 만들어 낸 것. "

월러스 & 그로밋의 어린이 재단은

내가 뭘하려고하는 기능에 <br />[br]을 변환하는 것입니다.

아이디어가 있으십니까?

+0

HTML을 원한다면 왜 '>'과 '<'으로 번역하고 있습니까? (> '& '<') –

+0

을 사용하지 않는 경우 [이 테스트] (http://regexr.com?2uu3p)를 참조하십시오. 어떻게'> '및'< '을 html로 변환 할 수 있습니까? – laukok

+0

'str_replace ('[br]', '
', $ string)'할 때 복잡한 정규식을 사용하는 이유는 무엇입니까? –

답변

3

에 한번 교체가 아니라 실제 문자보다 <>의 HTML 코드를 사용했다

$output = preg_replace('/\[(?:&nbsp;|\s)*([br]+)(?:&nbsp;|\s)*\]/', '&lt;$1 /&gt;', $string); 

$output = preg_replace('/\[(?:&nbsp;|\s)*([br]+)(?:&nbsp;|\s)*\]/', '<$1 />', $string); 

에 변경.

+0

감사합니다. 알았다. '< $ 1/>'- 그걸 html로 변환하려면 어떻게해야합니까? – laukok

+3

@lauthiamkok : 결과를 ['html_entity_decode'] (http://www.php.net/manual/en/function.html-entity-decode.php)에 전달하십시오. –

+0

감사합니다 Brad! – laukok

0

&lt;&gt;은 HTML 항목에 HTML 페이지에 <> 기호를 표시하는 데 사용됩니다. 결과가 유효한 html 태그를 갖기 위해서는 엔티티의 특정 문자를 사용해야합니다.