声明我有麻烦劈裂字符串如下: 什么是文本仍然是一个什么样的文字链接在锚转化的,哪些是来自YouTube的链接,将其转换为iframe标签。如果preg_replace函数功能
我的剧本是这样的:
$string='This is a link www.dinamomania.net this is a http://www.youtube.com/watch?v=U9LB6qGvdpQ';
echo makelink($string);
function makeLink($string){
/*** 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);
$string = preg_replace('/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/', '<iframe style= "margin:15px 0 15px 0;display:block;" width="500" height="300" src="http://www.youtube.com/embed/$7" frameborder="0" allowfullscreen></iframe>',$string);
/*** make all emails hot links ***/
$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);
return $string;
}
}
一切它的工作它是如何应该只是,我得到一个锚这样“>的iframe之前,那是因为第一它做链接到锚,然后。iframe的YouTube的格式,所以这是框内为锚
我一直在寻找一个if语句做这样的事情:
if(is link from youtube) {
// do the iframe part
} else {
// do the anchor part
}
任何帮助/建议将受到高度赞赏。谢谢 !
我已经想出了这样的事情:
if (preg_match("/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/", $string))
{
$string = preg_replace('/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/', '<iframe style= "margin:15px 0 15px 0;display:block;" width="500" height="300" src="http://www.youtube.com/embed/$7" frameborder="0" allowfullscreen></iframe>',$string);
} else {
$string = preg_replace('/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i','<a target="_blank" href="$1">$1</A>',$string);
}
但再次它做我的iframe的一部分,并没有与锚happends。
检查[**'preg_replace_callback()'**](http://php.net/manual/en/function.preg-replace-callback.php)函数。 – inhan