PHP preg_replace:将 Youtube 链接替换为替换 URL


PHP preg_replace: substituting Youtube links over substituting URLs

在我的网站上,我有三个正则表达式(一个被禁用,你会明白为什么......)来替换用户发布的内容:

1° 将电子邮件替换为链接的电子邮件:

$string = preg_replace("/(['w-?&;#~='.'/]+'@('[?)[a-zA-Z0-9'-'.]+'.([a-zA-Z]{2,3}|[0-9]{1,3})(']?))/i","<a href='"mailto:$1'">$1</a>",$string);

2° 将 Youtube 链接替换为嵌入视频:

$pattern = '/http:'/'/www'.youtube'.com'/watch'?(.*?)v=([a-zA-Z0-9_'-]+)('S*)/i';
$replace = '<iframe title="YouTube" class="youtube" type="text/html" width="320" height="240" src="http://www.youtube.com/embed/$2" frameborder="0" allowFullScreen></iframe>';
$string = preg_replace($pattern, $replace, $string);

3° 用链接替换链接(这些被禁用!

 /*** make sure there is an http:// on all URLs ***/
$string = preg_replace("/([^'w'/])(www'.[a-z0-9'-]+'.[a-z0-9'-]+)/i", "$1http://$2",$string);
/*** make all URLs links ***/
$string = preg_replace("/(['w]+:'/'/['w-?&;#~='.'/'@]+['w'/])/i","<a target='"_blank'" href='"$1'">$1</a>",$string);

我的问题是,显然,第三个在被以前的正则表达式替换后替换了 youtube 链接(切换顺序,中断 youtube 视频)

如何启用第三个正则表达式来替换忽略 youtube 模式的 URL?谢谢 ;-)

试试这个:

// youtube
$string = preg_replace("/'s*[a-zA-Z'/'/:'.]*youtube.com'/watch'?v=([a-zA-Z0-9'-_]+)([a-zA-Z0-9'/'*'-'_'?'&';'%'='.]*)/i"," <object width='"100%'" height='"344'"><param name='"movie'" value='"http://www.youtube.com/v/$1&hl=en&fs=1'"></param><param name='"allowFullScreen'" value='"true'"></param><embed src='"http://www.youtube.com/v/$1&hl=en&fs=1'" type='"application/x-shockwave-flash'" allowfullscreen='"true'" width='"100%'" height='"450'"></embed></object>  ",$string);
// email
$string = preg_replace("/(['w-?&;#~='.'/]+'@('[?)[a-zA-Z0-9'-'.]+'.([a-zA-Z]{2,3}|[0-9]{1,3})(']?))/i","<a href='"mailto:$1'">$1</a>", $string);
// links
$string = preg_replace("#(^|['n's>])(['w]+?://[^'s'"'n'r't<]*)#is", "''1<a href='"''2'">''2</a>", $string);