2011-04-11 6 views
0

모든 콜백을 강조하기 위해 약간의 콜백을 꿰매 었습니다. 정규 표현식이 나에게 엉덩이에 큰 고통을주고 있기 때문에 내 나이가 들었다.대괄호 (BBCode) 안에없는 모든 것에 콜백을 실행하십시오.

function highlight($str) { 
    return '<b>'.$str[0].'</b>'; 
} 

$str = '[b]Hello, World![/b] in either the color [blue]test[/blue] or [red]test[/red]'; 
$highlight = preg_replace_callback('|[[\/\!]*?[^\[\]]*?]|si', 'highlight', $str); 
echo $highlight; 

하지만 지금 내가 정말하고 싶은 반대 : 은 무엇 모든 다른하지만 ​​BBCodes 강조하기위한 정규식 것입니까?

답변

0

이것은 최선의 해결책은 아니지만 효과가 있습니다.

$re = '/ 
    (.*?)       # text before bBB...eBB 
     (\[(\w+?)\].*?\[\s*\/\3\]) # bBB...eBB 
    | 
    (.*?$)       # text after last bBB..eBB 
    /xui'; 

$string = "beginOfS [b]Hello, World![/b] in either the color [blue]test lorem [yel]ipsum[/yel] dolorem [/blue] or [red]test[/red] endOfS"; 

echo preg_replace($re, '<b>\1\4</b>\2', $string); 

// $nMatches = preg_match_all($re, $string, $aMatches); 

반환 :

<b>beginOfS </b>[b]Hello, World![/b]<b> in either the color </b>[blue]test lorem [yel]ipsum[/yel] dolorem [/blue]<b> or </b>[red]test[/red]<b> endOfS</b>