zoukankan      html  css  js  c++  java
  • 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('saysid=1')->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();

  • 相关阅读:
    3.16
    3.8
    3.7
    3.6
    3.5
    3.3
    3.2
    《机器学习十讲》学习报告一
    机器学习-集成算法
    机器学习-基于核的算法
  • 原文地址:https://www.cnblogs.com/passer101/p/6341654.html
Copyright © 2011-2022 走看看