2010-07-09 9 views
0

내 자신의 bbcode 파서를 만들고있어 재귀 따옴표를 만들려고 할 때 문제가 있습니다.PHP - BBCode parser - regex 및 preg_replace를 사용하는 재귀 [quote]

function forumBBCode($str){ 
$format_search=array(
'#\[quote=(.*?)\](.*?)\[/quote\]#is' 
); 

$format_replace=array(
'<blockquote class="quotearea"><i><a class="lblackbu" href="./index.php?status=userview&userv=$1">$1</a> wrote :</i><br />$2</blockquote>' 
); 

$str=preg_replace($format_search, $format_replace, $str); 
$str=nl2br($str); 
return $str; 
} 

내가 재귀 견적을 할 수있는 추가/수정을해야하는지 :

이 내 코드? 견적이 다른 따옴표 안에 즉 ... 도움

+1

정규 표현식을 처음 사용하는 경우 상당히 어렵습니다. 재귀 적 정규 표현식은 까다 롭기 때문에 대부분의 사람들은/e 평가 플래그를 선택하고 실제로는 정규식 "파서"가 포함 된 내용으로 내려 가게됩니다. 어쨌든, 먼저 http://www.regular-expressions.info/를 읽으십시오 - PHP 매뉴얼보다 더 나은 소개입니다. – mario

+0

나는이 안내서를 배울 것이고 나는 기능을하려고 노력할 것이다. 나는 너에게 알릴 것이다, 나는 네가 나를 어떻게 든 도울 수 있기를 바랍니다. tnx !!! – markzzz

+0

나는 그것을 해결할 수있는 두 가지 방법이 있다고 생각한다. a) 내용을 반복하고 열기 및 닫기 인용 태그를 검색하고 둘 다 발견되면 교체한다. (나머지는 보이는 bbcode로 남겨둔다.) b)'preg_replace_callback()'을 사용한다. 중첩 된 따옴표 태그가있는 경우 자체를 다시 호출합니다. – mgutt

답변

4

이 오래된 질문이다 그러나 나는] = PPL 어쨌든

$open = '<blockquote><span class="bold">Quote: </span><br />'; //the next few lines do the parsing for quote blocks. We 
     $close = '</blockquote>';          //have to do it outside the normal parsing arrays because that way does not allow nesting. 

     preg_match_all ('/\[quote\]/i', $str, $matches); 
     $opentags = count($matches['0']); 

     preg_match_all ('/\[\/quote\]/i', $str, $matches); 
     $closetags = count($matches['0']); 

     $unclosed = $opentags - $closetags; 
     for ($i = 0; $i < $unclosed; $i++) { 
       $str .= '</blockquote>'; 
     } 
//Do Quotes (nested) 
     $str = str_replace ('[quote]', $open, $str); 
     $str = preg_replace('/\[quote\=(.*?)\]/is','<blockquote class="darkbg"><span class="bold left">Quote: $1</span><br />', $str); 
     $str = str_replace ('[/quote]', $close, $str); 
return $str; 

평화를 내 솔루션을 게시합니다 : 그것은 전문적 더 비록

이것은 또한 당신을 관심 수도있다.

+2

닫히지 않은 태그에 대해주의를 기울였습니다. 미개봉 된 제품도 있다면 어떨까요? – wolo