如何将url转换为链接和youtube链接到iframe PHP?

时间:2013-09-02 14:41:41

标签: php iframe youtube

我想将任何youtube网址转换为iframe,并将任何网站网址转换为php中的超链接,我有代码,但它无法正常工作
这是我的字符串:

Google www.google.com is a best search engine , Google http://www.google.com is a best search engine. http://www.youtube.com/watch?v=BNHR6IQJGZs"
<br/><br/>

我想转换成:

<br/>
"Google &lt;a href="http://www.google.com"&gt;www.google.com&lt;/a&gt; is a best search engine , Google &lt;a href="http://www.google.com"&gt;www.google.com&lt;/a&gt; is a best search engine.  &lt;div align="center"&gt;
  &lt;iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/BNHR6IQJGZs?autoplay=0" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;
&lt;/div&gt;"


这是我的代码:

<?php
function ClickableLinksANDyoutubeIframe($text) {
    $v = $text;

    $v = @eregi_replace("\[yt\]http://www.youtube.com/watch\?v=([^\[]+)\[/yt\]","<iframe title=\"YouTube video player\" class=\"youtube-player\" type=\"text/html\" width=\"400\" height=\"260\" src=\"http://www.youtube.com/embed/\\1\" frameborder=\"0\" allowFullScreen></iframe>",$v);  

    $v = @eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
            '<a href="\\1" target=_blank>\\1</a>', $v);
    $v = @eregi_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
            '<a href="\\1" target=_blank>\\1</a>', $v);
    $v = @eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
    '\\1<a href="http://\\2" target=_blank>\\2</a>', $v);
    $v = @eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
    '<a href="mailto:\\1" target=_blank>\\1</a>', $v);

    return $v;
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

以下是解决方案:

<?php 
function ClickableLinksANDyoutubeIframe($text) {

    $v = $text;  
    $text = html_entity_decode($text);
    $text = strip_tags($text);
    $c="youtube";
    $v = @eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
            '<a href="\\1" target=_blank>\\1</a>', $v);
    $v = @eregi_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
            '<a href="\\1" target=_blank>\\1</a>', $v);
    $v = @eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
    '\\1<a href="http://\\2" target=_blank>\\2</a>', $v);
    $v = @eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
    '<a href="mailto:\\1" target=_blank>\\1</a>', $v);
    $v = preg_replace('#<a href="https?://www.'.$c.'.*?>([^>]*)</a>#i', '$1', $v);

   $v = @preg_replace("#http://(www\.)?youtube\.com/watch\?v=([^ &\n]+)(&.*?(\n|\s))?#i",
            '<div align="center"><object width="480" height="390"><param name="movie" value="http://www.youtube.com/v/$2"></param><embed src="http://www.youtube.com/v/$2" type="application/x-shockwave-flash" width="480" height="390"></embed></object></div>', $v);

  $v = @preg_replace("#https://(www\.)?youtube\.com/watch\?v=([^ &\n]+)(&.*?(\n|\s))?#i",
            '<div align="center"><object width="480" height="390"><param name="movie" value="http://www.youtube.com/v/$2"></param><embed src="http://www.youtube.com/v/$2" type="application/x-shockwave-flash" width="480" height="390"></embed></object></div>', $v);
    return $v;
}
?>