Home > Backend Development > PHP Tutorial > PHP substr interception string garbled problem solution [utf8 and gb2312]_PHP tutorial

PHP substr interception string garbled problem solution [utf8 and gb2312]_PHP tutorial

WBOY
Release: 2016-07-21 15:22:33
Original
1253 people have browsed it

substr --- Get part of the string
Syntax: string substr (string string, int start [, int length])
Description:
substr() returns a part of the string Part of the string, specified by the parameters start and length.
If start is a positive number, the returned string will start from the start character of string.
Example:

Copy code The code is as follows:

$rest = substr (" abcdef", 1); // returns "bcdef"
$rest = substr ("abcdef", 1, 3); // returns "bcd"
?>

If start is a negative number, the returned string will start from the start character at the end of string.
Example:
Copy code The code is as follows:

$rest = substr (" abcdef", -1); // returns "f"
$rest = substr ("abcdef", -2); // returns "ef"
$rest = substr ("abcdef", -3, 1); // returns "d"
?>

If the parameter length is given and it is a positive number, the returned string will be length characters from start.
If the parameter length is given and is a negative number, the returned string will end at the length-th character from the end of string.
Example:
Copy code The code is as follows:

$rest = substr (" abcdef", 1, -1); // returns "bcde"
?>

dongyue,2005-01-07 11:10:41
substr --- Get Partial string
Syntax: string substr (string string, int start [, int length])
Description:
substr() returns a part of string, specified by parameters start and length .
If start is a positive number, the returned string will start from the start character of string.
Example:
Copy code The code is as follows:

$rest = substr (" abcdef", 1); // returns "bcdef"
$rest = substr ("abcdef", 1, 3); // returns "bcd"
?>

If start is a negative number, the returned string will start from the start character at the end of string.
Example:
Copy code The code is as follows:

$rest = substr (" abcdef", -1); // returns "f"
$rest = substr ("abcdef", -2); // returns "ef"
$rest = substr ("abcdef", -3, 1); // returns "d"
?>

If the parameter length is given and it is a positive number, the returned string will be length characters from start.
If the parameter length is given and is a negative number, the returned string will end at the length-th character from the end of string.
Example:
Copy code The code is as follows:

$rest = substr (" abcdef", 1, -1); // returns "bcde"
?>

Chinese character interception function supported by Utf-8 and gb2312
Copy code The code is as follows:

//Intercept Chinese string
/*
Utf-8 and gb2312 are supported Chinese character interception function
cut_str(string, interception length, starting length, encoding);
Encoding defaults to utf-8
Starting length defaults to 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="Script House is a good website";
echo cut_str($str, 8, 5, 'gb2312');

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324655.htmlTechArticlesubstr --- Get part of the string syntax: string substr (string string, int start [, int length] ) Description: substr() returns a part of string, specified by the parameters start and length...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template