给你一个好的 <?php function c_substr($string, $from, $length = null){ preg_match_all('/[x80-xff]?./', $string, $match); if(is_null($length)){ $result = implode('', array_slice($match[0], $from)); }else{ $result = implode('', array_slice($match[0], $from, $length)); } return $result; } $str = "zhon华人min共和guo"; $from = 3; $length = 7; echo(c_substr($str, $from, $length)); // 输出: n华人min共 //还有utf-8的 /* Regarding windix's function to handle UTF-8 strings: one can use the "u" modifier on the regular expression so that the pattern string is treated as UTF-8 (available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32). This way the function works for other encodings too (like Greek for example). The modified function would read like this: */ function utf8_substr($str,$start) { $null = ""; preg_match_all("/./u", $str, $ar); if(func_num_args() >= 3) { $end = func_get_arg(2); return join($null, array_slice($ar[0],$start,$end)); } else { return join($null, array_slice($ar[0],$start)); } } ?> 要给分哦....
$str_one=substr($str,0,10);
echo $str_one;用php自带的substr函数,中文输出可能出现问题,因中文占两个字节,字符一个字节,建议利用码自己些一个完整函数,不会出现乱吗现象。
echo substr($str,0,13);//注意页面保存为utf-8时,使用这一个
<?php
function c_substr($string, $from, $length = null){
preg_match_all('/[x80-xff]?./', $string, $match);
if(is_null($length)){
$result = implode('', array_slice($match[0], $from));
}else{
$result = implode('', array_slice($match[0], $from, $length));
}
return $result;
}
$str = "zhon华人min共和guo";
$from = 3;
$length = 7;
echo(c_substr($str, $from, $length));
// 输出: n华人min共
//还有utf-8的
/*
Regarding windix's function to handle UTF-8 strings:
one can use the "u" modifier on the regular expression so that the pattern string is treated as UTF-8
(available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32).
This way the function works for other encodings too (like Greek for example).
The modified function would read like this:
*/
function utf8_substr($str,$start) {
$null = "";
preg_match_all("/./u", $str, $ar);
if(func_num_args() >= 3) {
$end = func_get_arg(2);
return join($null, array_slice($ar[0],$start,$end));
} else {
return join($null, array_slice($ar[0],$start));
}
}
?>
要给分哦....
//截取一定长度的字串
//substrgb(输入的字串,长度, 开始, 返回类型(1、暂空;2、去除html格式的字串))
if ( $type == 2) {
$str = strip_tags($str);
}
if(strlen($str) > $len) {
for ( $i=0; $i<$start+$len-2; $i++){
//截字符
$tmpstr =(ord($str[$i])>127)?$str[$i].$str[++$i]:$str[$i];
//累加字符
if ( $i>=$start) $tmp .= $tmpstr;
}
return $tmp.$more;
}
else {
return $str;
}
}
给你一个截取字符的函数 调用函数substrgb();
参数说明:
参数1:要截取的内容
参数2:要截取的长度
参数3:从第几个开始截取
描述
string mb_substr ( string str, int start [, int length [, string encoding]] )
具体查看手册
注意:linux下安装要使用参数--enable-mbstring=all
Windows下要在php.ini里打开php_mbstring.dll
echo substr($str,0,strlen("aaa中国人1"));
^_^
直接echo 'aaa中国人1';
嘿嘿,既然知道要什么就直接输出吧,大笑
有没有办法实现像asp.net substring一样输入开始位置为0,结束位置7 取出来的结果是"aaa中国人1"的效果呢?
$str_one=substr($str,0,-6);
echo $str_one;
mb_substr ( string str, int start [, int length [, string encoding]] ) 函数就符合你9楼的要求