知识学习

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 2750|回复: 0

destoon PHP在文章无缩略图时抓取单张或者多张文章内容图片

[复制链接]

升级   100%

152

主题

0

回帖

752

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
752
发表于 2020-9-15 23:53:14 | 显示全部楼层 |阅读模式
1.写一个方法专门做抓取图片。
  1. function getImgs($content,$order='ALL'){
  2.     $pattern="/<img.*?src=[\'|"](.*?(?:[\.gif|\.jpg]))[\'|"].*?[\/]?>/";
  3.     preg_match_all($pattern,$content,$match);
  4.     if(isset($match[1])&&!empty($match[1])){
  5.         if($order==='ALL'){
  6.             return $match[1];
  7.         }
  8.         if(is_numeric($order)&&isset($match[1][$order])){
  9.             return $match[1][$order];
  10.         }
  11.     }
  12.     return '';
  13. }
复制代码
2.利用查询出列表页的文章ID去查询内容
  1. $result = $db->query("SELECt ".$MOD['fields']." FROM {$table} WHERe $condition ORDER BY $order LIMIT $offset,$pagesize");
  2.     while($r = $db->fetch_array($result)) {
  3.         if($kw) $r['title'] = str_replace($kw, '<b class="f_red">'.$kw.'</b>', $r['title']);
  4.         $r['linkurl'] = mobileurl($moduleid, 0, $r['itemid']);
  5.         $r['date'] = timetodate($r[$time], $page < 4 ? 2 : 3);
  6.         $lists[] = $r;
  7.     }

  8.         // 获取内容
  9. foreach($lists as $key=>$val){
  10.     $t = $db->query("SELECT content FROM taomodi_article_data_21 WHERe itemid=".$val['itemid']);
  11.     while($res = $db->fetch_array($t)){
  12.         $content = video5($res['content']);
  13.         $content = str_replace('<hr class="de-pagebreak"/>', '', $content);
  14.         //调用方法
  15.         $val['content'] = getImgs($content);
  16.         $lists[$key] = $val;
  17.     }
  18. }
  19. // var_dump($lists);die;
复制代码
3.模板中使用
  1. {loop $lists $vv}
  2. <li>
  3.     <a href="{$vv[linkurl]}">
  4.         <div class="{if count($vv[content])==1 && !empty($vv[content])}article_left{/if}">
  5.         <dt>{$vv[title]}</dt>
  6.         <dd>{php $res = dsubstr($vv['introduce'],'80','...');echo str_replace(' ','',$res);}</dd>
  7.         </div>
  8.         <div class="{if count($vv[content])==1 && !empty($vv[content])}article_right{/if}">
  9.         <dd>
  10.             {if $vv[content]}
  11.             {loop $vv[content] $k $v}
  12.                 {php if($k >=3) break;}
  13.                 <img src="{$v}" id="{if count($vv[content])==1 && !empty($vv[content])}article_img{/if}"/>
  14.             {/loop}
  15.             {/if}
  16.         </dd>
  17.         </div>
  18.         <div class="clear"></div>
  19.     </a>
  20. </li>
  21. {/loop}
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|知识学习 ( 鲁ICP备20015483号 )

GMT+8, 2026-5-13 21:59 , Processed in 0.057180 second(s), 25 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表