0

我想将任何 youtube url 转换为 iframe 并将任何网站 url 转换为 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;
}

提前致谢。

4

1 回答 1

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;
}
?>

于 2013-09-03T15:28:57.097 回答