0
我正在PHP和CodeIgniter中創建一個自定義論壇。我正在嘗試編碼bbCode解析器,但是,我遇到了一些問題。我使用preg_replace_callback來替換[quote id = 123] [/ quote]標籤。如果只有一個引用級別,它會正常工作,但是一旦我嘗試「嵌套」引號標籤,所有內容都將被丟棄。當分析報價標籤時,它應該替換第一個開放報價標籤和最後一個關閉報價標籤。相反,它只解析第一個開引號標籤和第一個關閉引號標籤。任何人都可以爲此考慮解決辦法嗎?這是我的代碼:如何替換preg中的第一個和最後一個實例用PHP替換回調?
$str = "[quote id=123][quote id=456]This is the second level[/quote]This is the first level[/quote]";
$str = preg_replace_callback("^\[quote id=([0-9]+)\](.*?)\[/quote\]^", "_parse_quote", $str);
return $str;
function _parse_quote($matches){
$str = '';
$CI =& get_instance();
$query_message = "
SELECT
message_id, author_id, date_posted
FROM forum_messages
WHERE message_id = ".$matches[1]."
LIMIT 1";
if($query_message = $CI->db->query($query_message)){
if($query_message->num_rows() > 0){
$message = $query_message->row_array();
$CI->member->get_info($message['author_id']);
$author = $CI->member->info;
$str = '
<blockquote title="Originally posted by '.$author['display_name'].' about '.timespan(strtotime($message['date_posted']), time()).' ago...">
<p>'.$matches[2].'</p>
</blockquote>
';
}
}
return $str;
}