php获取文章的第一张图片

今天做东西的时候遇到一个问题就是如何把文章提取出来作为文章列表呢?

因为用了Ueditor,所以提交的数据包含了html标签。

搜索了一会找到了一个方案,用php自带的函数去掉了html标签。

$arr['comm'] = mb_substr(strip_tags($arr['art_content']),0,150,'utf-8');

strip_tags();去掉html,php标签,不过要注意的是当有选择性的去掉标签的时候要确保标签完整正确,否则会出现问题。

mb_substr();php自带的中文字符截取函数。

截取后再保存到表的另一个字段里面。

还有一个问题就是提取第一张图片作为略缩图:

使用了正则表达式:

     $info = D('Says')->where('says)->find();
        $temp=mt_rand(1,4);  
        $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.jpeg]))[\'|\"].*?[\/]?>/";  
        $content = $info['content'];  //文章内容  
        preg_match_all($pattern,$content,$matchContent);  
        print_r($matchContent);exit();

  

print_r($matchContent);exit();