2013-01-20 43 views
1

声明我有麻烦劈裂字符串如下: 什么是文本仍然是一个什么样的文字链接在锚转化的,哪些是来自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。

+0

检查[**'preg_replace_callback()'**](http://php.net/manual/en/function.preg-replace-callback.php)函数。 – inhan

回答

2

使用这种模式的URL

preg_replace('/([^\'"])((ht|f)tps?:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i', 
    '$1<A target="_blank" href="$2">$2</A>',$string); 

而且IFRAME之前发表声明A标签放置声明

/*** make all the IFrame links ***/ 
$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 URLs links ***/ 
$string = preg_replace('/([^\'"])((ht|f)tps?:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i', 
     '$1<A target="_blank" href="$2">$2</A>',$string); 

参见how it works

+0

Yeey!谢谢你的回答,这对我很有帮助。你是个天才! –

0

您可以使用正则表达式的方法:

if (preg_match("/http://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)(\w*)(&(amp;)?[\w\?=]*)?/i", $link)) 
{ 
    // It's a Youtube link! 
} 

但也可以使用这种方法:

$parsedlink = parse_url($link); 

if ($parsedlink['host'] == 'www.youtube.com') 
{ 
    // It's a Youtube link! 
} 
+0

Hi和感谢您的快速反应,我已经尝试过这一点,但这个想法是,我有超过1个链接,我不能解析所有网址或我不知道如何,因为我没有那么在PHP经验。 –

+0

所以首先你必须创建一个正则表达式得到所有的链接呈现在您的字符串...那么你修改逐一和你重建的字符串。 –