最简单的方法是做什么?
随机输入:
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>";
}
?>
我一直在搞乱它,只是想废弃它。希望有一种更简单的方法来做到这一点。
答案 0 :(得分:0)
$patterns = array(
array('pattern'=>'/mypattern/','replaceWith'=>'myreplacement\0','addClass'=>'myclass'),
array('pattern'=>'/mypattern/','replaceWith'=>'myreplacement\0','addClass'=>'myclass'),
);
foreach($patterns as $pattern) {
// .... do your thing here... once... for every possibility described in array above.
}