分享好友 教程资讯首页 频道列表

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

2023-10-08 16:061010

1.写一个方法专门做抓取图片。

  1. function getImgs($content,$order='ALL'){
  2.    $pattern="/<img.*?src=http://www.52419.net/skin/s11/image/nopic.gif>;
  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.        // 获取内容
  10. foreach($lists as $key=>$val){
  11.    $t = $db->query("SELECT content FROM taomodi_article_data_21 WHERe itemid=".$val['itemid']);
  12.    while($res = $db->fetch_array($t)){
  13.        $content = video5($res['content']);
  14.        $content = str_replace('<hr class="de-pagebreak"/>', '', $content);
  15.        //调用方法
  16.        $val['content'] = getImgs($content);
  17.        $lists[$key] = $val;
  18.    }
  19. }
  20. // 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}


反对 0
举报 0
收藏 0
打赏 0
评论 0
如何生成背景透明的微信小程序二维码
如何生成背景透明的微信小程序二维码透明背景参数is_hyaline 是否需要透明底色, is_hyaline 为true时,生成透明底色的小程序码publicfunctiongetQRCodeB($scene

0评论2023-10-08181

destoon标签调取相应的信息内容及图片
本教程合适于没有分表的情况下第一步:打开根目录/include/tag.func.php找到 isset($showpage) or $showpage = 0; 这句。在其上行或其下行添上(isset($showconte

0评论2023-10-08185

DESTOON搜索关键词记录过滤拼音以及全英文搜索
网站搜索关键词记录总有英文的恶意关键词搜索提交,导致destoon后台的搜索关键词记录一大堆垃圾无效的搜索关键词,所以就重新在官方默认的基础上修改过滤英文及

0评论2023-10-08151

DESTOON系统框架根据标题调取百度下拉+淘宝下拉词函数,增强页面相关性
写了一个destoon根据标题调取百度下拉+淘宝下拉词函数,增强页面相关性,也可以用于自动分词,提取相关词,适用于destoon任意版本,废话不多说。上代码!首先在/

0评论2023-10-08145