2011-02-10 100 views
0

我有一個字符串,如這樣的:正則表達式PHP找到BBCode的標籤內容

Land of gray [example]here is an example[/example] and pink. 

我掙扎着爬了PHP /正則表達式的代碼,將在[example]標籤的內容返回到一個變量.. 。

+0

你究竟如何掙扎? – 2011-02-10 10:57:25

+0

PHP有一個[BBCode解析器](http://de3.php.net/manual/en/book.bbcode.php),爲什麼不使用它? – Gordon 2011-02-10 11:56:32

回答

2

我不是一個PHP專家,但......這個表達式將工作

\[example\]([^\[]*)\[ 

,將捕獲的內容捕獲組。

所以你的示例內容應該在$ matches [1] ???

例如:

<?php 
$subject = "Land of gray [example]here is an example[/example] and pink."; 
$pattern = '/\[example\]([^\[]*)\[/'; 
preg_match($pattern, $subject, $matches); 
print_r($matches[1]); 
?> 

我沒有測試上面的代碼,因爲我沒有做這個機器上運行PHP的,但我認爲這工作...

0

BB代碼允許的屬性。 [url =「somelink」]點擊[/ url]將不會用這個正則表達式正確解析。我想給你一個答案,但我的正則表達式不起作用,這是我第一次來到這裏的原因。大聲笑。 ;-)