Preg从文本中替换soundcloud链接

时间:2012-05-14 22:36:53

标签: php replace soundcloud

$text='some text and text http://soundcloud.com/disclosuremusic/whats-in-your-head some other text http://soundcloud.com/bad-panda-records/indian-wells-04 and again text blabla'; 

$text = preg_replace('#(?:https?://).soundcloud\.com/([\-a-z0-9_]+/[\-a-z0-9_]+)#siU','<object height="81" width="100%"><param name="movie" value="http://player.soundcloud.com/player.swf?url=$1&amp;g=bb"></param><param name="allowscriptaccess" value="always"></param><embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=$1&amp;g=bb" type="application/x-shockwave-flash" width="100%"></embed></object> <a href="$1">$1</a>', $text);      

尝试从文本preg_replace soundcloud链接,但似乎没有运气。感谢

3 个答案:

答案 0 :(得分:1)

“。”在“soundcloud \ .com”失败之前

'%(?:https?://)(?:www\.)?soundcloud\.com/([\-a-z0-9_]+/[\-a-z0-9_]+)%im'

你需要考虑www。一点都没有?

答案 1 :(得分:0)

这样做:

$text = preg_replace('/((http?:\/\/)soundcloud\.com\/([\-a-z0-9_]+/[\-a-z0-9_]+))/i','<object height="81" width="100%"><param name="movie" value="http://player.soundcloud.com/player.swf?url=$1&amp;g=bb"></param><param name="allowscriptaccess" value="always"></param><embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=$1&amp;g=bb" type="application/x-shockwave-flash" width="100%"></embed></object> <a href="$1">$1</a>', $text);      

你在“soundcloud”之前有一个额外的点,而你没有正确逃脱。

答案 2 :(得分:0)

$text='some text and text http://soundcloud.com/qcntoz some other text http://soundcloud.com/qcntoz and again text blabla'; 

$text = preg_replace('#(?:https?://).soundcloud\.com/([\-a-z0-9_]+/[\-a-z0-9_]+)#siU','<object height="81" width="100%"><param name="movie" value="http://player.soundcloud.com/player.swf?url=$1&amp;g=bb"></param><param name="allowscriptaccess" value="always"></param><embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=$1&amp;g=bb" type="application/x-shockwave-flash" width="100%"> </embed></object> <a href="$1">$1</a>', $text);