我发布了youtube链接。
所以我想查找所有匹配项,将其替换为自定义链接并将其回显。
**这是样本帖子:
Life is good
{youtube}NgBZrRCPCH8{/youtube}
Fun is great!
{youtube}joRCPCH8{/youtube}
Earth is round and roll
{youtube}pwnkfH8{/youtube}**
Youtube链接就像json: 的 {的YouTube} NgBZrRCPCH8 {/ YouTube的}
像 youtube.com/NgBZrRCPCH8
之类的东西这是我一直在尝试的。
$con="Life is good
{youtube}NgBZrRCPCH8{/youtube}
Fun is great!
{youtube}joRCPCH8{/youtube}
Earth is round and roll
{youtube}pwnkfH8{/youtube}";
$pattern="/{youtube}/";
$replace="";
$first= preg_replace($pattern,$replace,$con);
$pat="{/youtube}";
$rep="";
$sec=preg_replace($pat,$rep,$first);
$patt="/{}/";
$repl="";
$last=preg_replace($patt,$repl,$sec);
if($last){echo '<div class="vid">
<div class="vendor">
<iframe width="300" height="200" src="http://www.youtube.com/embed/'.$last.'" frameborder="0" allowfullscreen></iframe>
</div>
</div>';}
感谢...
答案 0 :(得分:2)
我不确定你要做什么,但根据我的理解,我做了这个
$re = '/{youtube}(.*){\/youtube}/m';
$str="Life is good
{youtube}NgBZrRCPCH8{/youtube}
Fun is great!
{youtube}joRCPCH8{/youtube}
Earth is round and roll
{youtube}pwnkfH8{/youtube}";
preg_match_all($re, $str, $matches);
$re = '/([\w ]+)[^a-zA-Z {}]+(?!{)/m';
preg_match_all($re, $str, $matches1);
echo '<div class="vid">';
foreach ($matches[1] as $key => $value) {
echo '<div class="vendor">
'.$matches1[0][$key].'
<iframe width="300" height="200" src="http://www.youtube.com/embed/'.$value.'" frameborder="0" allowfullscreen></iframe>
</div>';
}
echo '</div>';
<强> 输出 强>
答案 1 :(得分:0)
另一种方式:
$trans = array ('{youtube}' => '<iframe width="300" height="200" src="http://www.youtube.com/embed/',
'{/youtube}' => '" frameborder="0" allowfullscreen></iframe></div><div class="vendor">');
$result = '<div class="vid"><div class="vendor">'
. substr(strtr($str, $trans), 0, -20) . '</div>';