2011-08-26 3 views
0

안녕하세요, 아주 기본적인 bbcode 파서를 사용하고 있습니다.중첩 된 bb 코드 인용 방법>

내 문제를 도와 줄 수 있습니까?

하지만, 예를 들어이 기록 된 경우 :

[quote=tanab][quote=1][code]a img{ 
text-decoration: none; 
}[/code][/quote][/quote] 

출력은 이것이다 :

tanab said: 
[quote=1] 
a img{ 
    text-decoration: none; 
} 
[/quote] 

가 어떻게

가서 그 문제를 해결까요? 전체 preg_replace 물건에 메신저 realllly 나쁜.

내 파서 :

function bbcode($input){ 
$input = htmlentities($input); 

$search = array(
      '/\[b\](.*?)\[\/b\]/is', 
      '/\[i\](.*?)\[\/i\]/is', 
      '/\[img\](.*?)\[\/img\]/is', 
      '/\[url=(.*?)\](.*?)\[\/url\]/is', 
      '/\[code\](.*?)\[\/code\]/is', 
      '/\[\*\](.*?)/is', 
      '/\\t(.*?)/is', 
      '/\[quote=(.*?)\](.*?)\[\/quote\]/is', 
); 

$replace = array(
      '<b>$1</b>', 
      '<i>$1</i>', 
      '<img src="$1">', 
      '<a href="$1">$2</a>', 
      '<div class="code">$1</div>', 
      '<ul><li>$1</li></ul>', 
      '&nbsp;&nbsp;&nbsp;&nbsp;', 
      '<div class="quote"><div class="quote-writer">$1 said:</div><div class="quote-body">$2</div></div>', 

); 

return preg_replace($search,$replace,$input); 

}

+2

하나님의 사랑을 위해, t BBCode 사용] (http://stackoverflow.com/questions/3788959/regex-to-split-bbcode-into-pieces/3792262#3792262)? – NullUserException

+0

무엇을 고치려고합니까? 문제가 무엇입니까? – afuzzyllama

+0

가능한 중첩 된 bb 코드가 중복되지 않습니다. (http://stackoverflow.com/questions/7198302/nested-bb-codes-wont-do-what-i-want) – mario

답변

0

재귀 정규식으로 적용 할 수 있습니다 :

'/\[quote=(.*?)\](((?R)|.*?)+)\[\/quote\]/is' 

적어도 보장합니다 출력 된 div가되지 않습니다 잘못 중첩 된 것. 하지만 모든 견적 블록을 잡으려면 두 번 또는 세 번 정규식을 실행해야합니다.

그렇지 않으면 preg_replace_callback으로 코드를 다시 작성해야합니다. 나는 이것을 선전하기 위해 성가 시게 할 수 없는데, 이것은 이미 수십 번 올랐기 때문에 (사이트 검색을 시도하십시오!), 이전에 해결되었습니다.