php怎么截取中文字符串

 更新时间:2016年11月25日 17:08  点击:1626
在php中截取字符串最简单的办法就是利用substr()函数来实现,但是substr函数只能截取英文,如果是中文不会是乱码哦,那么有朋友说可使用mb_substr()来截取,这个方法又不能截取中文英混合的字符。

此函数用于截取gb2312编码的中文字符串:

 代码如下 复制代码

<?php
// 说明:截取中文字符串
 
function mysubstr($str, $start, $len) {
    $tmpstr = "";
    $strlen = $start + $len;
    for($i = 0; $i < $strlen; $i++) {
        if(ord(substr($str, $i, 1)) > 0xa0) {
            $tmpstr .= substr($str, $i, 2);
            $i++;
        } else
            $tmpstr .= substr($str, $i, 1);
    }
    return $tmpstr;
}
?>

Utf-8、gb2312都支持的汉字截取函数

截取utf-8字符串函数

为了支持多语言,数据库里的字符串可能保存为UTF-8编码,在网站开发中可能需要用php截取字符串的一部分。为了避免出现乱码现象,编写如下的UTF-8字符串截取函数

关于utf-8的原理请看 UTF-8 FAQ

UTF-8编码的字符可能由1~3个字节组成, 具体数目可以由第一个字节判断出来。(理论上可能更长,但这里假设不超过3个字节)
第一个字节大于224的,它与它之后的2个字节一起组成一个UTF-8字符
第一个字节大于192小于224的,它与它之后的1个字节组成一个UTF-8字符
否则第一个字节本身就是一个英文字符(包括数字和一小部分标点符号)。

 代码如下 复制代码


<?php
// 说明:Utf-8、gb2312都支持的汉字截取函数
 
/*
Utf-8、gb2312都支持的汉字截取函数
cut_str(字符串, 截取长度, 开始长度, 编码);
编码默认为 utf-8
开始长度默认为 0
*/
 
function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
{
    if($code == 'UTF-8')
    {
        $pa = "/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/";
        preg_match_all($pa, $string, $t_string);
 
        if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
        return join('', array_slice($t_string[0], $start, $sublen));
    }
    else
    {
        $start = $start*2;
        $sublen = $sublen*2;
        $strlen = strlen($string);
        $tmpstr = '';
 
        for($i=0; $i<$strlen; $i++)
        {
            if($i>=$start && $i<($start+$sublen))
            {
                if(ord(substr($string, $i, 1))>129)
                {
                    $tmpstr.= substr($string, $i, 2);
                }
                else
                {
                    $tmpstr.= substr($string, $i, 1);
                }
            }
            if(ord(substr($string, $i, 1))>129) $i++;
        }
        if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";
        return $tmpstr;
    }
}
 
$str = "abcd需要截取的字符串";
echo cut_str($str, 8, 0, 'gb2312');
?>

注意明:

 代码如下 复制代码

function utf8Substr($str, $from, $len)
{
    return preg_replace('#^(?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$from.'}'.
                       '((?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$len.'}).*#s',
                       '$1',$str);
}

可单独截取uft8字符串哦。

程序说明:

1. len 参数以中文字符为标准,1len等于2个英文字符,为了形式上好看些

2. 如果将magic参数设为false,则中文和英文同等看待,取绝对的字符数

3. 特别适用于用htmlspecialchars()进行过编码的字符串

4. 能正确处理GB2312中实体字符模式(𖰰)

程序代码:

 代码如下 复制代码

function FSubstr($title,$start,$len="",$magic=true)
{
/**
* powered by Smartpig
* mailto:d.einstein@263.net
*/

$length = 0;
if($len == "") $len = strlen($title);

//判断起始为不正确位置
if($start > 0)
{
$cnum = 0;
for($i=0;$i<$start;$i++)
{
if(ord(substr($title,$i,1)) >= 128) $cnum ++;
}
if($cnum%2 != 0) $start--;

unset($cnum);
}

if(strlen($title)<=$len) return substr($title,$start,$len);

$alen = 0;
$blen = 0;

$realnum = 0;

for($i=$start;$i<strlen($title);$i++)
{
$ctype = 0;
$cstep = 0;
$cur = substr($title,$i,1);
if($cur == "&")
{
if(substr($title,$i,4) == "<")
{
$cstep = 4;
$length += 4;
$i += 3;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,4) == ">")
{
$cstep = 4;
$length += 4;
$i += 3;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,5) == "&")
{
$cstep = 5;
$length += 5;
$i += 4;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,6) == """)
{
$cstep = 6;
$length += 6;
$i += 5;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,6) == "'")
{
$cstep = 6;
$length += 6;
$i += 5;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(preg_match("/&#(d+);/i",substr($title,$i,8),$match))
{
$cstep = strlen($match[0]);
$length += strlen($match[0]);
$i += strlen($match[0])-1;
$realnum ++;
if($magic)
{
$blen ++;
$ctype = 1;
}
}
}else{
if(ord($cur)>=128)
{
$cstep = 2;
$length += 2;
$i += 1;
$realnum ++;
if($magic)
{
$blen ++;
$ctype = 1;
}
}else{
$cstep = 1;
$length +=1;
$realnum ++;
if($magic)
{
$alen++;
}
}
}

if($magic)
{
if(($blen*2+$alen) == ($len*2)) break;
if(($blen*2+$alen) == ($len*2+1))
{
if($ctype == 1)
{
$length -= $cstep;
break;
}else{
break;
}
}
}else{
if($realnum == $len) break;
}
}

unset($cur);
unset($alen);
unset($blen);
unset($realnum);
unset($ctype);
unset($cstep);

return substr($title,$start,$length);
}

本文章收集了在php中一些常常用到的正则表达式规则总结,有需要的朋友可参考。

匹配特定数字:

 代码如下 复制代码

^[1-9]d*$    //匹配正整数
^-[1-9]d*$   //匹配负整数
^-?[1-9]d*$   //匹配整数
^[1-9]d*|0$  //匹配非负整数(正整数 + 0)
^-[1-9]d*|0$   //匹配非正整数(负整数 + 0)
^[1-9]d*.d*|0.d*[1-9]d*$   //匹配正浮点数
^-([1-9]d*.d*|0.d*[1-9]d*)$  //匹配负浮点数
^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$  //匹配浮点数
^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$   //匹配非负浮点数(正浮点数 + 0)
^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$  //匹配非正浮点数(负浮点数 + 0)

评注:处理大量数据时有用,具体应用时注意修正

匹配特定字符串:

 代码如下 复制代码

^[A-Za-z]+$  //匹配由26个英文字母组成的字符串
^[A-Z]+$  //匹配由26个英文字母的大写组成的字符串
^[a-z]+$  //匹配由26个英文字母的小写组成的字符串
^[A-Za-z0-9]+$  //匹配由数字和26个英文字母组成的字符串
^w+$  //匹配由数字、26个英文字母或者下划线组成的字符串
评注:最基本也是最常用的一些表达式


匹配中文字符的正则表达式: [u4e00-u9fa5]

 代码如下 复制代码

$str = "php编程";
if (preg_match("/^[x{4e00}-x{9fa5}]+$/u",$str)) {
print("该字符串全部是中文");
} else {
print("该字符串不全部是中文");
}


评注:匹配中文还真是个头疼的事,有了这个表达式就好办了

匹配双字节字符(包括汉字在内):[^x00-xff]

 代码如下 复制代码

$str = "singlepoint单点日志";
if (preg_match("/^[x{4e00}-x{9fa5}]+$/u",$str)) {
print("该字符串全部是中文");
} else {
print("该字符串不全部是中文");
}


$alias_len = mb_strlen($value['alias'], "utf-8");
$temp_array = array();
for($i = 0;$i<$alias_len; $i++)
{
 $temp_array[$i] = mb_substr($value['alias'],$i,1,"utf-8");
 if(ord(substr($temp_array[$i],0,1))>'0xe0' && strlen($temp_array[$i])<3)
     $temp_array[$i] = '';

}
$value['alias'] = implode('',$temp_array);


评注:编码表 双字节字符编码范围 1. GBK (GB2312/GB18030) x00-xff GBK双字节编码范围 x20-x7f ASCII

xa1-xff 中文 gb2312 x80-xff 中文 gbk 2. UTF-8 (Unicode) u4e00-u9fa5 (中文) x3130-x318F (韩文

xAC00-xD7A3 (韩文) u0800-u4e00 (日文)

匹配空白行的正则表达式:ns*r

 代码如下 复制代码

$str = "123 456";
$patten = "/s+/";
$result = split($patten,$str);
echo join("<br>",$result);

评注:可以用来删除空白行

匹配HTML标记的正则表达式:<(S*?)[^>]*>.*?</1>|<.*? />
评注:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力

匹配首尾空白字符的正则表达式:^s*|s*$
评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式

匹配Email地址的正则表达式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
评注:表单验证时很实用

匹配网址URL的正则表达式:[a-zA-z]+://[^s]*
评注:网上流传的版本功能很有限,上面这个基本可以满足需求

匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
评注:表单验证时很实用

匹配国内电话号码:d{3}-d{8}|d{4}-d{7}
评注:匹配形式如 0511-4405222 或 021-87888822

匹配腾讯QQ号:[1-9][0-9]{4,}
评注:腾讯QQ号从10000开始

匹配中国邮政编码:[1-9]d{5}(?!d)
评注:中国邮政编码为6位数字

匹配身份证:d{15}|d{18}
评注:中国的身份证为15位或18位

匹配ip地址:d+.d+.d+.d+
评注:提取ip地址时有用

可能对你有用的与正则表达式有关的内容

一,a-z A-Z_0-9 //最常见的字符
二,(bfw)(sda) //用圆括号包含起来的单元符号,一个括号表示一个整体
三,[sdwe][^mjnb] //用方括号包含起来的原子表,原子表中^代表排除或相反内容
四,转义字符
d 包含所有的数字[0-9]
D 除所有数字外[^0-9]
w 包含所有英文字符[a-zA-Z_0-9]
W 除所有英文字符外[^a-zA-Z_0-9] -----匹配特殊字符
s 包含空白区域 如回车,换行,分页等[fnr]

4,正则表达式元字符

* 匹配前一个内容的0次或多次
. 匹配内容的0次或多次,但不包含回车换行
+ 匹配前一个内容的1次或多次
? 匹配前一个内容的0次或1次
| 选择匹配,类似php中||的用法
^ 匹配字符串首部的内容
$ 匹配字符串尾部内容
b 匹配单词边界,边界可以是空格或者特殊符号
B 匹配除带单词边界的意外内容
{m} 匹配前一个内容的重复次数为m次
{m,} 匹配前一个内容的重复次数大于等于m次
{m,n} 匹配前一个内容的重复次数m次到n次
() 合并整体匹配,并放入内存,可使用\1\2依次获取调用

无限级分类是所有程序开发中会碰到的一个问题,下面我来介绍php+mysql实现的一个无限级分类程序,有需要的朋友可参考参考。

下面给大家看看我的数据库结构吧:数据库的名字为:fa_category

Field Type Comment
cid int(11) 分类id
catename varchar(255) 分类名字
catetype int(1) 分类类型,1为单页面,2为普通分类
catdir varchar(255) 英文目录
display int(1) 1为显示,2为不显示
keywords varchar(255) 栏目关键字
description text 栏目描述
ctime int(11) 创建时间
parentid int(11) 父节点id,最高节点父节点为0

     我们使用一个parentid字段来记录父节点的id,如果parentid为0,则为root。不多说,我们看看代码怎么写吧。我们要实现的功能就是如图片所示:

              

   怎么样把它这样显示呢?这个问题我想了很久,先看看这段SQL语句吧,

 代码如下 复制代码

SELECT c.cat_id, c.cat_name, c.measure_unit, c.parent_id, c.is_show, c.show_in_nav, c.grade         ,c.sort_order, COUNT( s.cat_id ) AS has_children
FROM ecs_category AS c
LEFT JOIN ecs_category AS s ON s.parent_id = c.cat_id
GROUP BY c.cat_id
ORDER BY c.parent_id, c.sort_order ASC

 用左连接连接一个表,返回一个字段 has_children,这个字段是记录有多少子节点。

   看看代码吧,

 代码如下 复制代码
public function getCategory($catid=0,$re_type = true,$selected=0)
    {
        $db      =  new Public_DataBase();
        $sql  =  'select c.cid,c.catename,c.catetype,c.ctime,c.parentid,count(s.cid) as has_children from '.
        __MYSQL_PRE.'category as c left join '.
        __MYSQL_PRE.'category as s on s.parentid=c.cid group by c.cid order by c.parentid asc';
        $res          =  $db->selectTable($sql);
        $cateInfo     =    self::getChildTree($catid,$res);
        if($re_type==true)
        {
               $select   =    '';
              foreach($cateInfo as $val)
              {
                 $select .= '<option value="' . $val['cid'] . '" ';
                 $select .= ($selected == $val['cid']) ? "selected='ture'" : '';
                 $select .= '>';
                  if($val['level']>0)
                  {         
                   $select .= str_repeat('&nbsp;', $val['level'] * 4);
                  }
                  $select .= htmlspecialchars(addslashes($val['catename']), ENT_QUOTES) . '</option>';
              }
              return $select;
        }
       
        else
        {
            foreach($cateInfo as $key=>$val)
            {
                  if($val['level']>0)
                  {         
                           $cateInfo[$key]['catename']    =    "|".str_repeat('&nbsp;', $val['level'] * 8)."└─".$val['catename'];
                         
                  }
            }
            return $cateInfo;
        }
       
       
    }
   
    /**
     * 通过父ID递归得到所有子节点树
     * @param int $catid  上级分类
     * @param array $arr  含有所有分类的数组
     * @return array
     */
    public function getChildTree($catid,$arr)
    {
        $level = $last_cat_id = 0;
        while (!empty($arr))
        {
            foreach($arr as $key=>$value)
            {
                $cid     =  $value['cid'];
                if ($level == 0 && $last_cat_id == 0)
                {
                   if ($value['parentid'] > 0)
                   {
                       break;
                   }
                   $options[$cid]          =     $value;
                   $options[$cid]['level'] =     $level;
                   $options[$cid]['id']    =     $cid;
                   $options[$cid]['name']  =     $value['catename'];
                   unset($arr[$key]);
                   if ($value['has_children'] == 0)
                   {
                        continue;
                   }
                   $last_cat_id  = $cid;
                   $cat_id_array = array($cid);
                   $level_array[$last_cat_id] = ++$level;
                   continue;
    
                }
                if ($value['parentid'] == $last_cat_id)
                {
                        $options[$cid]          = $value;
                        $options[$cid]['level'] = $level;
                        $options[$cid]['id']    = $cid;
                        $options[$cid]['name']  = $value['catename'];
                        unset($arr[$key]);
                        if ($value['has_children'] > 0)
                        {
                            if (end($cat_id_array) != $last_cat_id)
                            {
                                $cat_id_array[] = $last_cat_id;
                            }
                                $last_cat_id    = $cid;
                                $cat_id_array[] = $cid;
                                $level_array[$last_cat_id] = ++$level;
                        }
                }
                elseif ($value['parentid'] > $last_cat_id)
                {
                    break;
                }
            }
           
             $count = count($cat_id_array);
             if ($count > 1)
             {
                 $last_cat_id = array_pop($cat_id_array);
                
             }
             elseif ($count == 1)
             {
               if ($last_cat_id != end($cat_id_array))
               {
                   $last_cat_id = end($cat_id_array);
               }
               else
               {
                    $level = 0;
                    $last_cat_id = 0;
                    $cat_id_array = array();
                    continue;
               }
             }
             if ($last_cat_id && isset($level_array[$last_cat_id]))
             {
                $level = $level_array[$last_cat_id];
             }
             else
             {
                 $level = 0;
             }
        }
                    return $options;

    }用smarty的一个循环就可以把它显示出来 效果和上面图片的一样!大家有什么问题可以给我留言。

一个简单php和mysql数据分页程序有需要的朋友可参考一下。
 代码如下 复制代码


<?php
// Adam's Custom PHP MySQL Pagination Tutorial and Script
// You have to put your mysql connection data and alter the SQL queries(both queries)
// This script is in tutorial form and is accompanied by the following video:
mysql_connect("DB_Host_Here","DB_Username_Here","DB_Password_Here") or die (mysql_error());
mysql_select_db("DB_Name_Here") or die (mysql_error());
//////////////  QUERY THE MEMBER DATA INITIALLY LIKE YOU NORMALLY WOULD
$sql = mysql_query("SELECT id, firstname, country FROM myTable ORDER BY id ASC");
//////////////////////////////////// Adam's Pagination Logic ////////////////////////////////////////////////////////////////////////
$nr = mysql_num_rows($sql); // Get total of Num rows from the database query
if (isset($_GET['pn'])) { // Get pn from URL vars if it is present
    $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); // filter everything but numbers for security(new)
    //$pn = ereg_replace("[^0-9]", "", $_GET['pn']); // filter everything but numbers for security(deprecated)
} else { // If the pn URL variable is not present force it to be value of page number 1
    $pn = 1;
}
//This is where we set how many database items to show on each page
$itemsPerPage = 10;
// Get the value of the last page in the pagination result set
$lastPage = ceil($nr / $itemsPerPage);
// Be sure URL variable $pn(page number) is no lower than page 1 and no higher than $lastpage
if ($pn < 1) { // If it is less than 1
    $pn = 1; // force if to be 1
} else if ($pn > $lastPage) { // if it is greater than $lastpage
    $pn = $lastPage; // force it to be $lastpage's value
}
// This creates the numbers to click in between the next and back buttons
// This section is explained well in the video that accompanies this script
$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
} else if ($pn == $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;';
} else if ($pn > 1 && $pn < $lastPage) {
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
    $centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
    $centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
}
// This line sets the "LIMIT" range... the 2 values we place to choose a range of rows from database in our query
$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
// Now we are going to run the same query as above but this time add $limit onto the end of the SQL syntax
// $sql2 is what we will use to fuel our while loop statement below
$sql2 = mysql_query("SELECT id, firstname, country FROM myTable ORDER BY id ASC $limit");
//////////////////////////////// END Adam's Pagination Logic ////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Adam's Pagination Display Setup /////////////////////////////////////////////////////////////////////
$paginationDisplay = ""; // Initialize the pagination output variable
// This code runs only if the last page variable is ot equal to 1, if it is only 1 page we require no paginated links to display
if ($lastPage != "1"){
    // This shows the user what page they are on, and the total number of pages
    $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '&nbsp;  &nbsp;  &nbsp; ';
    // If we are not on page 1 we can place the Back button
    if ($pn != 1) {
        $previous = $pn - 1;
        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
    }
    // Lay in the clickable numbers display here between the Back and Next links
    $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
    // If we are not on the very last page we can place the Next button
    if ($pn != $lastPage) {
        $nextPage = $pn + 1;
        $paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
    }
}
///////////////////////////////////// END Adam's Pagination Display Setup ///////////////////////////////////////////////////////////////////////////
// Build the Output Section Here
$outputList = '';
while($row = mysql_fetch_array($sql2)){

    $id = $row["id"];
    $firstname = $row["firstname"];
    $country = $row["country"];

    $outputList .= '<h1>' . $firstname . '</h1><h2>' . $country . ' </h2><hr />';
   
} // close while loop
?>
<html>
<head>
<title>Adam's Pagination</title>
<style type="text/css">
<!--
.pagNumActive {
    color: #000;
    border:#060 1px solid; background-color: #D2FFD2; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:link {
    color: #000;
    text-decoration: none;
    border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:visited {
    color: #000;
    text-decoration: none;
    border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:hover {
    color: #000;
    text-decoration: none;
    border:#060 1px solid; background-color: #D2FFD2; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:active {
    color: #000;
    text-decoration: none;
    border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
-->
</style>
</head>
<body>
   <div style="margin-left:64px; margin-right:64px;">
     <h2>Total Items: <?php echo $nr; ?></h2>
   </div>
      <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>
      <div style="margin-left:64px; margin-right:64px;"><?php print "$outputList"; ?></div>
      <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>
</body>
</html>

效果

Page 6 of 39          Back   4    5    6    7    8    Next

 

本文章利用Ajax分页来简单讲述一下如何利用php与ajax实现数据无刷新分页功能,有需要的朋友可参考一下。

简单的mysql数据表结构

 代码如下 复制代码

CREATE TABLE messages
(
msg_id INT PRIMARY KEY AUTO_INCREMENT,
message VARCHAR(150)
);

JavaScript 代码

这里是ajax前段利用jquery来处理

 代码如下 复制代码

<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{

function loading_show()
{
$('#loading').html("<img src='images/loading.gif'/>").fadeIn('fast');
}

function loading_hide()
{
$('#loading').fadeOut();
}

function loadData(page)
{
loading_show();
$.ajax
({
type: "POST",
url: "load_data.php",
data: "page="+page,
success: function(msg)
{
$("#container").ajaxComplete(function(event, request, settings)
{
loading_hide();
$("#container").html(msg);
});
}
});
}
loadData(1); // For first time page load default results
$('#container .pagination li.active').live('click',function(){
var page = $(this).attr('p');
loadData(page);
});
});

</script>

load_data.php

这里是获取由ajax发送的数据然后经过php查询mysql返回信息

 代码如下 复制代码

<?php
if($_POST['page'])
{
$page = $_POST['page'];
$cur_page = $page;
$page -= 1;
$per_page = 15; // Per page records
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;
include"db.php";
$query_pag_data = "SELECT msg_id,message from messages LIMIT $start, $per_page";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$msg = "";
while ($row = mysql_fetch_array($result_pag_data))
{
$htmlmsg=htmlentities($row['message']); //HTML entries filter
$msg .= "<li><b>" . $row['msg_id'] . "</b> " . $htmlmsg . "</li>";
}
$msg = "<div class='data'><ul>" . $msg . "</ul></div>"; // Content for Data
/* -----Total count--- */
$query_pag_num = "SELECT COUNT(*) AS count FROM messages"; // Total records
$result_pag_num = mysql_query($query_pag_num);
$row = mysql_fetch_array($result_pag_num);
$count = $row['count'];
$no_of_paginations = ceil($count / $per_page);
/* -----Calculating the starting and endign values for the loop----- */

//Some Code. Available in download script

}
?>

[!--infotagslink--]

相关文章

  • js URLdecode()与urlencode方法支持中文解码

    下面来介绍在js中来利用urlencode对中文编码与接受到数据后利用URLdecode()对编码进行解码,有需要学习的机友可参考参考。 代码如下 复制代码 ...2016-09-20
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • c#中判断字符串是不是数字或字母的方法

    这篇文章介绍了C#判断字符串是否数字或字母的实例,有需要的朋友可以参考一下...2020-06-25
  • PostgreSQL判断字符串是否包含目标字符串的多种方法

    这篇文章主要介绍了PostgreSQL判断字符串是否包含目标字符串的多种方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-23
  • 详解C++ string常用截取字符串方法

    这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • php字符串按照单词逐个进行反转的方法

    本文实例讲述了php字符串按照单词进行反转的方法。分享给大家供大家参考。具体分析如下:下面的php代码可以将字符串按照单词进行反转输出,实际上是现将字符串按照空格分隔到数组,然后对数组进行反转输出。...2015-03-15
  • MySQL 字符串拆分操作(含分隔符的字符串截取)

    这篇文章主要介绍了MySQL 字符串拆分操作(含分隔符的字符串截取),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-22
  • C#实现字符串转换成字节数组的简单实现方法

    这篇文章主要介绍了C#实现字符串转换成字节数组的简单实现方法,仅一行代码即可搞定,非常简单实用,需要的朋友可以参考下...2020-06-25
  • 使用list stream: 任意对象List拼接字符串

    这篇文章主要介绍了使用list stream:任意对象List拼接字符串操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-09
  • 关于Mysql中文乱码问题该如何解决(乱码问题完美解决方案)

    最近两天做项目总是被乱码问题困扰着,这不刚把mysql中文乱码问题解决了,下面小编把我的解决方案分享给大家,供大家参考,也方便以后自己查阅。首先:用show variables like “%colla%”;show varables like “%char%”;这两条...2015-11-24
  • C# 16 进制字符串转 int的方法

    这篇文章主要介绍了C# 16 进制字符串转 int的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • 获取中文字符串的实际长度代码

    JS中默认中文字符长度和其它字符长度计算方法是一样的,但某些情况下我们需要获取中文字符串的实际长度,代码如下: 复制代码 代码如下: function strLength(str) { var realLength = 0, len = str.length, charCode = -1;...2014-06-07
  • C#读取中文文件出现乱码的解决方法

    这篇文章主要介绍了C#读取中文文件出现乱码的解决方法,涉及C#中文编码的操作技巧,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • PostgreSQL 字符串处理与日期处理操作

    这篇文章主要介绍了PostgreSQL 字符串处理与日期处理操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-01
  • Windows服务器MySQL中文乱码的解决方法

    我们自己鼓捣mysql时,总免不了会遇到这个问题:插入中文字符出现乱码,虽然这是运维先给配好的环境,但是在自己机子上玩的时候咧,总得知道个一二吧,不然以后如何优雅的吹牛B。...2015-03-15
  • Mysql在debian系统中不能插入中文的终极解决方案

    在debian环境下,彻底解决mysql无法插入和显示中文的问题Linux下Mysql插入中文显示乱码解决方案mysql -uroot -p 回车输入密码进入mysql查看状态如下:默认的是客户端和服务器都用了latin1,所以会乱码。解决方案:mysql>use...2013-10-04
  • php 中英文混合字符串截取

    文章介绍一个实用的函数,我们如果用php substr来截取字符在中文上处理的很有问题,今天自己写了一个比较好的中文与英文字符截取的函数,有需要的朋友可以参考下。 ...2016-11-25
  • linux mint 下mysql中文支持问题

    一.mysql默认不支持中文,它的server和db默认是latin1编码.所以我们要将其改变为utf-8编码,因为utf-8包含了地球上大部分语言的二进制编码 1.关闭mysql服务 sudo /etc/init.d/mysql stop 2.修改mysql配置文件 mysql配...2015-10-21
  • C#实现对字符串进行大小写切换的方法

    这篇文章主要介绍了C#实现对字符串进行大小写切换的方法,涉及C#操作字符串的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • c#将字节数组转成易读的字符串的实现

    这篇文章主要介绍了c#将字节数组转成易读的字符串的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25