温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php正则匹配类

发布时间:2020-07-16 09:10:59 来源:网络 阅读:506 作者:Lee_吉 栏目:web开发
  1. 代码:
    /**
    * @desc:正则匹配类
    * @author [Lee] <[<complet@163.com>]>
    * @method
    * 1、geturl         获取所有超链接
    * 2、getimg         获取所有图片
    * 3、getaudio           获取所有音频文件
    * 4、getvideo           获取所有视频文件
    * 5、getparagraph       获取所有段落
    * 6、getuser            获取自定义内容         preg 自定义正则,如:/<h2>(.*)<h2>/Ui
    */
    class match{
    private $content = '';
    /*
     @desc:构造方法,初始化待匹配文本
     */
    public function __construct($content){
        $this->content = $content;
    }
    /*
     @desc:获取所有超链接
     @return:所有匹配的超链接
     */
    public function geturl(){
        $content = $this->content;
        $preg = '/<[a|A].*?href=[\'\"]{0,1}([^>\'\"\ ]*).*?>/i';
        $bool = preg_match_all($preg,$content,$res);
        $urls = array();
        if($bool){
            $urls = $res[1];
        }
        return array_unique($urls);
    }
    /*
     @desc:获取所有图片
     @return:所有匹配的图片
     */
    public function getimg(){
        $content = $this->content;
        $preg="/(src)=(\\\?)([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png|svg))\\2\\3/i";
        $bool = preg_match_all($preg,$content,$res);
        $imgs = array();
        if($bool){
            $imgs = $res[4];
        }
        return array_unique($imgs);
    }
    /*
     @desc:获取所有音频文件
     @return:所有匹配的音频文件
     */
    public function getaudio(){
        $content = $this->content;
        $preg="/(src)=(\\\?)([\"|']?)([^ \"'>]+\.(mp3|wav|wma|ogg|ape|acc))\\2\\3/i";
        $bool = preg_match_all($preg,$content,$res);
        $audios = array();
        if($bool){
            $audios = $res[4];
        }
        return array_unique($audios);
    }
    /*
     @desc:获取所有视频文件
     @return:所有匹配的视频文件
     */
    public function getvideo(){
        $content = $this->content;
        $preg="/(src)=(\\\?)([\"|']?)([^ \"'>]+\.(swf|flv|mp4|rmvb|avi|mpeg|ra|ram|mov|wmv)((\?[^ \"'>]+)?))\\2\\3/i";
        $bool = preg_match_all($preg,$content,$res);
        $videos = array();
        if($bool){
            $videos = $res[4];
        }
        return array_unique($videos);
    }
    /*
     @desc:获取所有段落文本
     @return:所有匹配的段落文本
     */
    public function getparagraph(){
        $content = $this->content;
        $preg="/<p>(.*)<\/p>/Ui";
        $bool = preg_match_all($preg,$content,$res);
        $paragraphs = array();
        if($bool){
            $paragraphs = $res[1];
        }
        return array_unique($paragraphs);
    }
    /*
     @desc:获取所有自定义内容
     @return:所有匹配的自定义内容
     */
    public function getuser($preg){
        $content = $this->content;
        $bool = preg_match_all($preg,$content,$res);
        $users = array();
        if($bool){
            $users = $res[1];
        }
        return array_unique($users);
    }
    }
  2. 测试:
    $match = new match($str);
    $ret = $match->getimg();
    var_dump($ret);
  3. 输出:
    array(7) {
    [0]=>
    string(61) "https://assets-cdn.github.com/images/search-shortcut-hint.svg"
    [1]=>
    string(69) "https://assets-cdn.github.com/images/spinners/octocat-spinner-128.gif"
    [2]=>
    string(75) "https://assets-cdn.github.com/images/spinners/octocat-spinner-32-EAF2F5.gif"
    [3]=>
    string(68) "https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif"
    [14]=>
    string(45) "https://badges.gitter.im/walkor/Workerman.svg"
    [15]=>
    string(48) "https://cache.yisu.com/upload/information/20200310/52/106945.jpg"
    [16]=>
    string(42) "http://donate.workerman.net/img/donate.png"
    }
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI