php支持gb2312,uft-8中英文字符截取函数

 更新时间:2016年11月25日 17:40  点击:1303

php教程支持gb2312,uft-8中英文字符截取函数

<?php
//截取gb2312中文字符串
function mysubstr($str, $start, $len) {
    $tmps教程tr = "";
    $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;
}
?>

<?php
//截取utf8字符串
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);
}
?>

把上面两个例子整合了

<?php
/*
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');
?>

php教程设置北京时间函数date_default_timezone_set()
定义和用法
date_default_timezone_set() 函数设置用在脚本中所有日期/时间函数的默认时区。

语法
date_default_timezone_set(timezone)

<?php
$now = time();
date_default_timezone_set('america/new york');
print date('c', $now);
date_default_timezone_set('europe/paris');
print date('c', $now);
?>

再看一个例子

<?php

date_default_timezone_set('america/denver');

$summer = mktime(12,0,0,7,4,2008);
print date('c', $summer) . "n";

date_default_timezone_set('america/phoenix');
print date('c', $summer) . "n";
?>

注释:自 php 5.1.0 起(此版本日期时间函数被重写了),如果时区不合法则每个对日期时间函数的调用都会产生一条 e_notice 级别的错误信息,如果使用系统设定或 tz 环境变量则还会产生 e_strict 级别的信息。


参考表

表格   h-10.   others

cet   cst6cdt   cuba   eet   egypt  
eire   est   est5edt   etc/gmt   etc/gmt+0  
etc/gmt+1   etc/gmt+10   etc/gmt+11   etc/gmt+12   etc/gmt+2  
etc/gmt+3   etc/gmt+4   etc/gmt+5   etc/gmt+6   etc/gmt+7  
etc/gmt+8   etc/gmt+9   etc/gmt-0   etc/gmt-1   etc/gmt-10  
etc/gmt-11   etc/gmt-12   etc/gmt-13   etc/gmt-14   etc/gmt-2  
etc/gmt-3   etc/gmt-4   etc/gmt-5   etc/gmt-6   etc/gmt-7  
etc/gmt-8   etc/gmt-9   etc/gmt0   etc/greenwich   etc/uct  
etc/universal   etc/utc   etc/zulu   factory   gb  
gb-eire   gmt   gmt+0   gmt-0   gmt0  
greenwich   hongkong   hst   iceland   iran  
israel   jamaica   japan   kwajalein   libya  
met   mst   mst7mdt   navajo   nz  
nz-chat   poland   portugal   prc   ps教程t8pdt  
roc   rok   singapore   turkey   uct  
universal   utc   w-su   wet   zulu  

其项目地址: php教程-debug-tools/">http://freshmeat.net/projects/php-debug-tools/
文件下载地址: http://freshmeat.net/urls/7c58ae3fecce5763e7546b958d36e082
目前是1.03版本


这里偶的环境是window xp , apache2.2, php5.2+ zend optimizer,
这里结合php debug tools的帮助文档来讲解,图有些是摘自文档.

一.安装篇
安装前的准备环境:必须得先装x-debug,
至于怎样安装x-debug请看http://www.xdebug.org/docs/install

1. 从http://www.xdebug.org/download.php下载合适你的x-debug版本
2. 解压dll文件到php安装目录下的ext目录,如c:/php/ext/php_xdebug-2.0.4-5.2.8-nts.dll
3. 修改php.ini文件,加入下段:
-------------偶是变态的分割线,你看不见我------------------------
zend_extension = "c:/php/ext/php_xdebug-2.0.4-5.2.8-nts.dll"
xdebug.collect_includes = off
xdebug.default_enable = off

xdebug.dump_globals = off
xdebug.dump_once = off
xdebug.extended_info = off
-------------偶是变态的分割线,你看不见我------------------------
注:this example is for non-thread safe version. for the thread safe version change "zend_extension" to "zend_extension_ts"

安装完毕,解压php debug tools压缩包里的所有文件到网站发布目录.
(假设发布目录为c:www,那么就在其新建一个debug目录,把所有文件扔进去)

在浏览器中输入:http://localhost/debug/test1-debug.php
如果看见下图则安装成功.

二.调试篇
1.debug errors
如以下代码:
复制代码 代码如下:
<?php
require './lib/debug.php';
function test($a, $b)
{
echo $asd;
}
test(10, 'abc');
?>


2.用debug()来调试
如以下代码:
复制代码 代码如下:
<?php
require './lib/debug.php';
function test($args)
{
test_nested($args);
}
function test_nested($args)
{
debug($args);
// or: debug(get_defined_vars());
// or: debug();
}
test(array('id'=>123, 'str'=>'test'));
?>


3.用dump()或者dump_tofile()调试
如以下代码:
复制代码 代码如下:
<?php
include_once './lib/dump.php';
function test5()
{
include './testdata/test0.php';
$test = array('int'=>1, 'float'=>2.0, 'float2'=>2.1);
dump($test, $_server);
}
function test1() { test2(); }
function test2() { test3(); }
function test3() { test4(); }
function test4() { test5(); }
test1();
?>


至于dump_tofile()一般在以下情形使用:
a.当你不想停止程序运行时
b.不是你不想显示调式数据,而是你不能.比如当你在ajax请求状态时.
c.你还想在多处地方调式

可参见debug目录下的test7-dump_tofile.php

注:本人在运行dump()或者dump_tofile()时发现并不能出现php debug tool文档中所述

这里可以通过修改debug/lib/debug.php的代码来更正.(因为dump_tofile()有调用到dump(),所以我们只需修改一处.
于149行处的
echo $pre;

修改成:

//edit by benben---start
echo '<script type="text/网页特效">';
echo 'document.write(';
echo $pre;
echo ');';
echo '</script>';
//edit by benben---end

修正后的图:

4.跟踪代码,查看系统性能
可以浏览目录下的test3-trace.php,之后点右下角的控制台就可以了.
具体可参照文档.(文档在压缩包内的doc目录下)
三,如何与项目结合?

先把php debug tool的解压缩文件,放置于项目目录,建个目录就叫debug吧! : )
其实我们需要的只是几个文件而已.
比如路径为:c:wwwprojectnamedebug

之后,我们可以通过两种方式来调试
第一种,可以在项目源码中加入这么一句:
include_once('./lib/debug.php');

例如以下:c:wwwprojectnamehellodebugindex.php
复制代码 代码如下:
<?php
include_once('./debug/lib/debug.php');

$faint = 'helloworld ,debuging';

debug($arrb);
?>


什么?你不想每个页面都写这么一句?
那么看看第二种方法,
这里也有两种方式,
1.修改php.ini 加入以下内容(修改成你自身的目录):
auto_prepend_file = "c:wwwprojectnamedebugauto_prepend.php"
auto_append_file = "c:wwwprojectnamedebugauto_append.php"

2.修改.htaccess文件(注:此方法偶没试过,嘿嘿)
php_value auto_prepend_file "c:wwwprojectnamedebugauto_prepend.php"
php_value auto_append_file "c:wwwprojectnamedebugauto_append.php"

开始研究gvim的配置文件(_vimrc), 现在给大家看一下我产生乱码之前的配置

配置文件里enconding,fileeconding,fileecondings的含义:
encoding: gvim 内部使用的字符编码方式,包括 vim 的 buffer (缓冲区)、菜单文本、消息文本等。
用户手册上建议只在 .vimrc 中改变它的值,事实上似乎也只有在 .vimrc 中改变它的值才有意义。
fileencoding: gvim 中当前编辑的文件的字符编码方式,vim 保存文件时也会将文件保存为这种字符编码方式 (不管是否新文件都如此)。
fileencodings: gvim 启动时会按照它所列出的字符编码方式逐一探测即将打开的文件的字符编码方式,并且将 fileencoding 设置为最终探测到的字符编码方式。
因此最好将 unicode 编码方式放到这个列表的最前面,将拉丁语系编码方式 latin1 放到最后面。
其中:chinese 就是 cp963编码
只到这里我突然想起来,我的浏览里默认的字符集是 gbk的,而_vimrc里的设置编码是utf-8的,二者不对应,

 

colors desert
set nobackup
set guifont=courier_new:h12:cansi
"处理文本中显示乱码
set encoding=utf-8
set fileencodings=chinese
set fileencoding=chinese
"处理菜单及右键菜单乱码
source $vimruntime/delmenu.vim
source $vimruntime/menu.vim
"处理consle输出乱码
language messages zh_cn.utf-8
syntax enable
syntax on

fatal error: cannot redeclare这种问题php教程 开发可能会碰到过,原因是很简单的,就是重复调用了相同名字的函数,

function mydate($format='y-m-d h:i:s',$timest=0)
{
 global $cfg_cli_time;
 $addtime = $cfg_cli_time * 3600;
 if(empty($format))
 {
  $format = 'y-m-d h:i:s';
 }
 return gmdate ($format,$timest+$addtime);
}

面我的

require_once(dedeinc."/inc/inc_fun_funstring.php");


也包含了一个名为mydate的函数如果现在我们这样用就出现如

a.php

require_once(dedeinc."/inc/inc_fun_funstring.php");
function mydate($format='y-m-d h:i:s',$timest=0)
{
 global $cfg_cli_time;
 $addtime = $cfg_cli_time * 3600;
 if(empty($format))
 {
  $format = 'y-m-d h:i:s';
 }
 return gmdate ($format,$timest+$addtime);
}

就会出现fatal error: cannot redeclare mydate() (previously declared in

[!--infotagslink--]

相关文章

  • php 中英文混合字符串截取

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

    这篇文章主要介绍了C#实现中英文混合字符串截取的方法,是C#字符串操作的常用方法,需要的朋友可以参考下...2020-06-25
  • 字符串截取函数开始

    字符串截取函数开始 function csubStr($str,$start,$len) { $strlen=strlen($str); $clen=0; ...2016-11-25
  • PHP针对中英文混合字符串长度判断及截取方法示例

    这篇文章主要介绍了PHP针对中英文混合字符串长度判断及截取方法,结合实例形式分析了php中英文字符串的遍历、转换、截取、计算等相关操作技巧,需要的朋友可以参考下...2017-04-03
  • 发一个刚编的暴力版/温柔版中文截取函数

    推荐使用暴力版的, 安全可靠; 温柔版的从程序编写的角度看比较高效. 呵呵 基本原理是修正 off, len 可能的错位, 温柔版是从 off 往前倒寻, 寻到第一个 <0xa0 的字...2016-11-25
  • php mb_strwidth函数实现中英文混排字符串截取

    如果是简单的字符截取我们常用的就substr函数或再使用mb_substr来截取字符了,但是我们有时会发现有中英文混排字符串截取时并不像那么简单,因这样我们需要考虑到编码问...2016-11-25
  • C#中英文混合字符串截取函数

    这篇文章介绍了C#中英文混合字符串截取函数,有需要的朋友可以参考一下...2020-06-25
  • php mb_strlen()中英混体字符截取代码

    注:如果在用mb_strlen出现fatal error: call to undefined function mb_strlen,这种问题你要可以用php教程info()看一下有没有装载mbstring,如果没有,尝试将php_mbstring....2016-11-25
  • php 中英文字符串截取函数

    1 /** 2 * php教程获取字符串中英文混合长度 3 * @param $str string 字符串 4 * @param $$charset string 编码 5 * @return 返回长度,1中文=1位,2英文=1...2016-11-25
  • php 字符串截取函数

    本文章介绍的是php自带的一个截取字符串的函数,只能处理英文,数字的不能截取中文混排的哦,有需要的朋友可以参考,后面的比较好用,第一个主要是给初学者学学用的。 ...2016-11-25
  • 字符串截取函数(支持中英文混体)

    字符串截取函数(支持中英文混体) 以前我们截取字符串都会用php自带的函数,今天我来看一下一款字符串截取函数(支持中英文混体)的php代码实例吧。 字符串截取函数(支持中...2016-11-25
  • php中文汉字截取函数

    要截取的字符串, $num要截取的长度, 返回截取的字符串 代码如下 复制代码 public function substrgb($in,$num) { //$num=16;...2016-11-25
  • 利用PHP函数计算中英文字符串长度

    大家知道英文字符占一个字节,而中文字符gbk占两个字符,utf8占三个字符,很多人印象中php计算字符串长度就是strlen()函数,其实不然,它计算的是字节的长度而非字符的长度,那么...2016-11-25
  • php 中文字符截取函数

    这是一款专业用来处理php 中文字符裁取函数哦,解决了中文字符截取会出现筹码的问题哦。 global $charset; if(strlen($string) <= $length) { return $string...2016-11-25
  • 中英文字符串截取函数(包括html)

    这里提供一款支持中文汉字与英文混合在一起的截取功能,包括对html标签等进来处理,下面我们来看看这款截取函数吧。 中英文字符串截取函数(包括html) function...2016-11-25
  • php 字符截取与图片过滤函数

    php 字符截取与图片过滤函数 本文章免费为各位朋友提供一款哦,如果你喜欢的话不防进来看看这款图片过滤正则表达试 function msubstr($str, $start, $len) { $tm...2016-11-25
  • php 字符截取函数

    php 字符截取函数 function sub_str($title,$lengh){ if(strlen($title)>$lengh) { $pp=2; $len=strlen($title); if($len%2<>0) {$pp=1;} $title=...2016-11-25
  • php使用字符串截取函数从结尾删除字符串

    字符串截取我们这里有简单的办法,通常如果只对于简单的可以使用substr函数了,如果要删除字符串中的字符我们也可以使用substr函数了,下面来看看。 修复了一个获取控...2016-11-25
  • PHP 中英文混合排版中处理字符串常用的函数

    /* 我们在处理中文数据时,经常要处理一些情况,下面就是针对 这些情况,我做的一些函数,已经用在了实践中 如果有问题,请与我联系 OICQ: 86804 */ # 判断某个位置是中...2016-11-25
  • php 字符截取函数 substr 与自定中英文截取函数

    本文章要讲的是关于php的substr函数与自己写了一个中英文截取函数哦,关于首先我们来看看substr这个函数的使用方法吧。 substr实例 $content ='i love you www.11...2016-11-25