php 字符截取函数
<?php
function sub_str($title, $lengh) {
if (strlen($title) > $lengh) {
$pp = 2;
$len = strlen($title);
if ($len % 2 <> 0) {
$pp = 1;
}
$title = substr($title, 0, $lengh - $pp);
$title = $title . ' …';
}
return $title;
}
//--------------------------------------字符截取函数2
function sub($Modi_Str, $start, $length, $mode = false) {
$n = 0;
for ($i = 0; $i < $start; $i++) {
if (ord(substr($Modi_Str, $i, 1)) > 0xa0) {
if ($mode) {
$start++;
$i++;
}
$n++;
}
}
if (!$mode) $start = $start + $n % 2;
$The_length = $start + $length;
for ($i = $start; $i < $The_length; $i++) {
if (ord(substr($Modi_Str, $i, 1)) > 0xa0) {
$The_Str.= substr($Modi_Str, $i, 2);
$i++;
if ($mode) $The_length++;
} else {
$The_Str.= substr($Modi_Str, $i, 1);
}
}
return $The_Str;
}本文地址:http://www.phprm.com/code/php-substr.html
转载随意,但请附上文章地址:-)