外链检查

来源:互联网 发布:织梦模板html源码 编辑:程序博客网 时间:2024/05/17 12:51
function checkIfOutlink($content) {    if (!$content) {        return false;    }    $content = preg_replace("/(style|id|class)=\"[\s\S]*?\"/i","",$content); //过滤style标签    //下面正则可以根据情况来选择    $regex = '@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|[a-z.\-]+[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))@';    $regex = '/(https?:)?(\/\/)?([\da-z\.-]+)\.([a-z]{2,6})/i';    if (preg_match_all($regex , $content, $match)) {        $suffixs = array('jpg','jpeg','bmp','gif','psd','png','JPG','JPEG','BPM','GIF','PSD','PNG');        foreach ($match[0] as $v) {            $url = trim($v);            $url = str_replace(array('"/'), '', $url);            $suffix = strtolower(array_pop(explode('.', $url)));            if (!in_array($suffix, $suffixs)) {                return $v;                break;            }        }    }    return false;}