首页 > 编程技术 > php

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

发布时间:2016-11-25 17:40

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--]

您可能感兴趣的文章: