[英]php bbcode preg match all
我正在嘗試編寫一個函數,該函數將檢查打開的bbcode標簽和關閉的bbcode標簽。
到目前為止,這是我寫的內容:
public function CheckBBCodes($argText)
{
$openQuoteTagCounter = preg_match_all('[quote]', $argText, $openQuoteTagCounter);
$closeQuoteTagCounter = preg_match_all('#\[\/quote\]\s*#', $argText, $closeQuoteTagCounter);
if($openQuoteTagCounter > $closeQuoteTagCounter)
{
echo "There are more open quote tags than close quote tags!";
}
if($closeQuoteTagCounter > $openQuoteTagCounter)
{
echo "There are more close quote tags than open quote tags!";
}
}
沒用 我忘記了什么?
最突出的是您的正則表達式模式不正確...
這個:
$openQuoteTagCounter = preg_match_all('[quote]', $argText, $openQuoteTagCounter);
$closeQuoteTagCounter = preg_match_all('#\[\/quote\]\s*#', $argText, $closeQuoteTagCounter);
應該:
$openQuoteTagCounter = preg_match_all('#\[quote\]#i', $argText, $openQuoteTagCounter);
$closeQuoteTagCounter = preg_match_all('#\[/quote\]#i', $argText, $closeQuoteTagCounter);
可以進一步改進:
$openQuoteTagCounter = preg_match_all('#\[quote\]#i', $argText);
$closeQuoteTagCounter = preg_match_all('#\[/quote\]#i', $argText);
如果您有[QUOTE]
類的標簽,則使用i
使其不區分大小寫。
您無需使用正斜杠( /
),因為您使用#
作為分隔符。
您不需要preg_match_all
的第三個參數,因為您沒有對其進行任何操作,並且無論如何都將其覆蓋...
我還建議您使用以下代碼結構
if(X > Y){
//Do stuff...
}
else if(Y > X){
//Do other stuff...
}
else{
//Do different stuff...
}
相對於if(...){...} if(...){...} if(...){...} if(...){...}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.