0
我想解析一個BBCode報價與正則表達式。我的引用有一些變量,我想將它們存儲在PHP變量中,以便稍後在代碼中使用它們。正則表達式preg_match - 未定義的偏移量與填充數組
我BBCode的報價是這樣的:
[quote=user;post_id] TEXT [/quote]
我的PHP代碼:
function parseQuote($text) {
$pattern = '/\[quote=(.*?);(.*?)\](.*?)\[\/quote\]/'; // quote regex
preg_match($pattern, $text, $match);
$quote_text = $match[3]; // get the text
$replace = '
<blockquote class="posttext-blockquote">
<div class="posttext-blockquote-top">
<i class="fa fa-caret-up"></i> $1 said:
</div>
<div class="posttext-blockquote-bottom">
'.$quote_text.'...
</div>
</blockquote>'; // make the replace text, $quote_text will be used to check things later
return preg_replace($pattern,$replace,$text); // return parsed
}
我現在面臨的問題是,即使$匹配陣列裝滿數據,它仍然給我Notice: Undefined offset: 3
警告,但在我的網頁上,它實際上給我的報價文字。
當我做print_r($match)
它給我這個:
Array ([0] => [quote=poepjejan1;15]Why can't I be a piggy :([/quote] [1] => poepjejan1 [2] => 15 [3] => Why can't I be a piggy :() 1
我已經嘗試了所有種類的東西,但它一直給我undifined偏移誤差。我在這裏做錯了什麼?
P.S.我是新來的正則表達式。
在IDEONE上,沒有警告:https://ideone.com/Ej7Pxm –
嘗試使用您自己的錯誤處理程序並查看錯誤的確切位置,如http://stackoverflow.com/a/5373824/3882707 – Ionut
@Ionut是的,我剛剛發現我忘了添加一個錯誤處理程序,謝謝你告訴我:) –