墨文院 发文

从文章内容中获取第一张图片作为缩略图

+关注 逍遥涛子

从文章内容中获取第一张图片作为缩略图

<?php
  $link = mysqli_connect('127.0.0.1','root','root'); //注意,主机域名务必写为127.0.0.1如果是localhost肯定会出幺蛾子!!!
 
 if ($link)
 {
     mysqli_set_charset($link,'utf8');
     mysqli_select_db($link,"test");
     $sql =  "select * from posts where id between 293 and 318";
     $res = mysqli_query($link,$sql);
     while($rows = mysqli_fetch_assoc($res)){
        $soContent = $rows['post_content'];
        preg_match_all("/<img.*\>/isU",$soContent,$ereg);//正则表达式把图片的整个都获取出来了
        $img=$ereg[0][0];//图片
        $p="#src=('|\")(.*)('|\")#isU";//正则表达式
        preg_match_all ($p, $img, $img1);
        $img_path =$img1[2][0];//获取第一张图片路径
        if(!$img_path){
          echo "ID:".$rows['ID']."无图片";
          $img_path="images/nopic.jpg";  //如果新闻中不存在图片,用默认的nopic.jpg替换 */
        }else{
          echo "ID:".$rows['ID']."的图片地址:".$img_path;
        }
        echo "<br/>";   
   }
 };
?>

4424012-d4b04bdaad8bfa31.webp.jpg

我要反馈

本文链接:http://www.71us.com/p/161276993542.html
下一篇:php使用PHPAnalysis提取关键字中文分词