2013-10-23 19 views
1
$string = preg_replace("_\[soundcloud\]/http:\/\/soundcloud.com\/(.*)/\[/soundcloud\]_is", "<iframe width=\"100%\" height=\"166\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=\$0\"></iframe>", $string); 

你好再次Stackoverflow!Soundcloud PHP ubb分析器

我想我的UBB解析器使用preg_replace以上支持的SoundCloud的鏈接,通過解析

[soundcloud](url)[/soundcloud]

<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url= (url) "></iframe> 

,但是這是行不通的。

有人可以幫助我什麼是我的正則表達式錯誤?

在此先感謝!

回答

2

你的模式不是很好的逃脫。

  1. 由於您使用的分隔符是不是/,你不需要逃避所有的斜線。和關閉方括號中不需要進行轉義:

    ~\[soundcloud]http://soundcloud.com/(.*)/\[/soundcloud]~is

  2. 要捕獲您使用的是貪婪量詞*的URL。如果字符串中有多個[soundcloud]標記,則會出現問題,因爲捕獲將在最後一個結束標記處停止。爲了解決這個問題,你可以使用一個懶惰的量詞*?

    ~\[soundcloud]http://soundcloud.com/(.*?)/\[/soundcloud]~is

    你可以試試這個太:

    ~\[soundcloud]http://soundcloud.com/([^/]+)/\[/soundcloud]~i

  3. 你捕捉是第一個捕獲小組。那麼他的參考是$1而不是$0這是整場比賽。

  4. 爲了您的替換字符串使用簡單的報價,以避免內部逃避所有的雙引號:

    '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=$1"></iframe>'

+0

與'〜\ [的SoundCloud] https://soundcloud.com解決它( 。*?)\ [/ soundcloud]〜is &&