phpcms 自动截取文章描述

Posted by phper star | Posted in CMS技巧 | Posted on 2012-7-16 10:33

 

<?php

header('Content-type: text/html; charset=utf-8');

 

// 连接,选择数据库

$link = mysql_connect('localhost', 'root', '123456')

    or die('Could not connect: ' . mysql_error());

echo 'Connected successfully';

mysql_select_db('newwc') or die('Could not select database');

mysql_query("SET NAMES UTF8");

 

// 查出content表中description字段为空的contentid和news表中文章没有描述得文章内容

$query = "SELECT a.contentid,b.content FROM ylb_content a,ylb_c_news b where a.description = '' and a.contentid=b.contentid";

$result = mysql_query($query) or die('Query failed: ' . mysql_error());

 

 

//更新content表中的描述

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {

   $sql="update ylb_content set description ='".csubstr($line['content'],0,100)."' where contentid =".$line['contentid']; 

   mysql_query($sql);

}

 

// 释放结果集

mysql_free_result($result);

 

// 关闭连接

mysql_close($link);

 

 

 function p($array){

echo('<pre>');

print_r($array);

echo('</pre>');

}

 

//截取urf-8字符串

function csubstr($str, $start=0, $length, $charset="utf-8", $suffix=false) 

    { 

        if(function_exists("mb_substr")) 

        { 

        if(mb_strlen($str, $charset) <= $length) return $str; 

        $slice = mb_substr($str, $start, $length, $charset); 

        } 

        else 

        { 

        $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; 

        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; 

        $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; 

        $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"; 

        preg_match_all($re[$charset], $str, $match); 

        if(count($match[0]) <= $length) return $str; 

        $slice = join("",array_slice($match[0], $start, $length)); 

        } 

        if($suffix) return $slice."…"; 

        return $slice; 

    } 

标签: phpcms 自动截取文章描述 批量为w 批量为文章自动截取描述