分享好友 教程首页 教程搜索 频道列表

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

2018-11-25 16:338121
+关注28
核心提示:1.写一个方法专门做抓取图片。function getImgs($content,$order='ALL')...

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}

本文标签: #多图 #内容图
免责声明:凡注明来源本网的所有作品,均为本网合法拥有版权或有权使用的作品,欢迎转载,注明出处。非本网作品均来自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。
生成海报
相关评论1
您可能在找更多

DESTOON搜索页随关键词调用广告位 单个广告下所有广告 代码

    用于搜索页的广告调用 其中: 调用 单个独立的广告id 不管是什么位置的广告都调用 单个广告 aid=64 数字自己对应广告修改 这个只能显示4个广告。 pagesize=4 随搜索关键词 and key_word='$keyword'

小黑 模板相关2017-07-06

下一篇
我来说两句
1