首页 > 教程 > 前端技术 > 二开教程

    收藏

    PHP资讯无缩略图时抓取单张或者多张文章内容图片
    小黑2023-04-19二开教程8℃
    广告
    核心提示:1.写一个方法专门做抓取图片。function getImgs($content,$order='ALL'){$pattern="/img.*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?/";preg_match_all($pattern,$content,$match);if(isset($match[1])!empty($match[1])){if($order===&
    1.写一个方法专门做抓取图片。
    function getImgs($content,$order='ALL'){
        $pattern="/<img.*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";
        preg_match_all($pattern,$content,$match);
        if(isset($match[1])&&!empty($match[1])){
            if($order==='ALL'){
                return $match[1];
            }
            if(is_numeric($order)&&isset($match[1][$order])){
                return $match[1][$order];
            }
        }
        return '';
    }

    2.利用查询出列表页的文章ID去查询内容
    $result = $db->query("SELECt ".$MOD['fields']." FROM {$table} WHERe $condition ORDER BY $order LIMIT $offset,$pagesize");
        while($r = $db->fetch_array($result)) {
            if($kw) $r['title'] = str_replace($kw, '<b class="f_red">'.$kw.'</b>', $r['title']);
            $r['linkurl'] = mobileurl($moduleid, 0, $r['itemid']);
            $r['date'] = timetodate($r[$time], $page < 4 ? 2 : 3);
            $lists[] = $r;
        }
     
            // 获取内容
    foreach($lists as $key=>$val){
        $t = $db->query("SELECT content FROM taomodi_article_data_21 WHERe itemid=".$val['itemid']);
        while($res = $db->fetch_array($t)){
            $content = video5($res['content']);
            $content = str_replace('<hr class="de-pagebreak"/>', '', $content);
            //调用方法
            $val['content'] = getImgs($content);
            $lists[$key] = $val;
        }
    }
    // var_dump($lists);die;

    3.模板中使用
    {loop $lists $vv}
    <li>
        <a href="{$vv[linkurl]}">
            <div class="{if count($vv[content])==1 && !empty($vv[content])}article_left{/if}">
            <dt>{$vv[title]}</dt>
            <dd>{php $res = dsubstr($vv['introduce'],'80','...');echo str_replace(' ','',$res);}</dd>
            </div>
            <div class="{if count($vv[content])==1 && !empty($vv[content])}article_right{/if}">
            <dd>
                {if $vv[content]}
                {loop $vv[content] $k $v}
                    {php if($k >=3) break;}
                    <img src="{$v}" id="{if count($vv[content])==1 && !empty($vv[content])}article_img{/if}"/>
                {/loop}
                {/if}
            </dd>
            </div>
            <div class="clear"></div>
        </a>
    </li>
    {/loop}

 
  • 文本链接:https://mbtxw.com/Course/show.php?itemid=154
  • 文章来源:模板开发网
  • 版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
标签: 多图 内容图
0相关评论

    QQ交流群

    模板天下网QQ群

  • 欢迎DT爱好者加群学习交流:68503649

    联系关注

  • MBTXW.COM腾讯微博
  • 536205792QQ号
  • 68503649加QQ群
  • 536205792@qq.com客服邮箱