执行以下操作的最简单方法是什么?
随机输入:
check out my new video here http://www.youtube.com/watch?v=123abc
or here http://youtu.be/123abc
here is my new wallpaper http://somepage.com/fRDTk.jpg
and my new homepage http://google.com
输出:
check out my new video here
<a class="youtube" href="http://www.youtube.com/watch?v=123abc">
http://www.youtube.com/watch?v=123abc</a>
or here
<a class="youtube" href="http://youtu.be/123abc">
http://youtu.be/123abc</a>
here is my new wallpaper
<a class="image" href="http://somepage.com/fRDTk.jpg">
http://somepage.com/fRDTk.jpg</a>
and my new homepage
<a class="iframe" href="http://google.com">
http://google.com</a>
自动链接并根据链接添加特定类的最简单方法是什么?(图片,youtube,其他)
我已经把它放在一起了,但它失败了。
<?php
foreach ($imgur->captions->item as $comment) {
$co = $comment->caption;
$linkstring = preg_replace('/(http|ftp)?+(s)?:?(\/\/)?+(www.)?((\w|\.)+)+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU|be|info|co)+(\/)?(\S+)?/i', '<a rel="fancybox fancybox.iframe" href="\0">\0</a>', $co );
if(preg_match('/^http:\/\/(?:www\.)?(?:youtube.com|youtu.be)\/(?:watch\?(?=.*v=([\w\-]+))(?:\S+)?|([\w\-]+))$/i', $co, $vresult)) {
$pattern = "/(http|ftp)?+(s)?:?(\/\/)?+(www.)?((\w|\.)+)+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU|be|info|co)+(\/)?(\S+)?/i";
$replacement = '<a class="fancybox-media" href="\0">\0</a>';
$text = preg_replace($pattern, $replacement, $co);
$type= 'youtube';
}
elseif(preg_match('/(http(s?):)?([\/.|\w|\s])*\.(?:jpg|gif|png|jpeg|bmp)/i', $co, $vresult)) {
$pattern = "/(http|ftp)?+(s)?:?(\/\/)?+(www.)?((\w|\.)+)+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU|be|info|co)+(\/)?(\S+)?/i";
$replacement = '<a class="fancybox" href="\0">\0</a>';
$text = preg_replace($pattern, $replacement, $co);
$type= 'image';
}
else {
$pattern = "/(http|ftp)?+(s)?:?(\/\/)?+(www.)?((\w|\.)+)+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU|be|info|co)+(\/)?(\S+)?/i";
$replacement = '<a class="fancybox fancybox.iframe" href="\0">\0</a>';
$text = preg_replace($pattern, $replacement, $co);
$type = 'none';
}
echo "<div class=\"icomment\">
<p class=\"icomment\">“",$text,"” -",$comment->author,"</p>
</div>";
}
?>
我一直在搞砸它,只想报废它。希望有一个更简单的方法来做到这一点。