我试图在字符串中搜索Youtube网址和mp3网址...并用播放器替换每个字符串。
以下是代码:
$patterns = array();
$patterns[0] = '#(http://)(www.)(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})#x';
$patterns[1] = '((https?:\/\/)?(\w+?\.)+?(\w+?\/)+\w+?.(mp3|ogg))';
$replacements = array();
$replacements[0] = '<iframe width="640" height="385" src="http://www.youtube.com/embed/\\3" frameborder="0" allowfullscreen></iframe>';
$replacements[1] = '<a id="m5" class="audio {skin:"green", autoPlay:false, addShadow:false,addGradientOverlay:true}" href="\\0">Beaver Radio Network</a>';
$brnpost = 'This is our story content. Here is the youtube link: <br /><br /> http://www.youtube.com/watch?v=qvwefWgIQhY <br /><br />Listen Here: http://beaverradionetwork.com/audio/1011/brnpodcasts/leonhardt_skaar_2015.mp3';
echo preg_replace($patterns, $replacements, $brnpost);
`
如果我单独运行每个正则表达式,它就有效。如果我将它们加入到数组中,它将接收部分mp3链接并将其放入Youtube嵌入中。像这样:
This is our story content. Here is the youtube link
<br /><br />
<iframe width="640" height="385" src="http://www.youtube.com/embed/leonhardt_s" frameborder="0" allowfullscreen>
</iframe>kaar_2015.mp3
</body>
Youtube嵌入显示了mp3替换的部分......而其余的mp3替换正在mp3链接中显示。当放在一起时,这两种模式使它们都失败了。
我做错了什么?