BeginnerThe following PHP substr library function program is not perfect, but the general Chinese (GB18030,GB2312,BIG5) is not a problem. This function is not suitable for utf-8 encoded text.
- $str string
- $max Maximum number of characters
- function Substring ($STR, $max) {
- $ CNT = 0 ;//Actual Count
- $ Index = 0 ;//Current Index
- $ Output = '' ;//Output
- //
- While ($cnt<$max && $index<strlen($str)) {
- $ output. = $str [$index];
- Big5
- if (Ord ($str [$index])>=0x81 &&
Ord ($str [$index])<=0xfe) {
- if ($index +1<strlen($str)) {
- if (Ord ($str [$index +1])>=0x40
&& Ord ($str [$index +1])<0x7e)
- || (Ord ($str [$index +1]) > =0xa1
&& Ord ($str [$index +1])<=0xfe)) {
- $index + +;
- $ output. = $str [$index];
- }
- }
- }
- gb2312
- else if (Ord ($str [$index])>=0xa1
&& Ord ($str [$index])<=0xf7) {
- $ output. = $str [$index];
- if ($index +1<strlen($str)) {
- if (Ord ($str [$index +1])>=0xa1
&& Ord ($str [$index +1])<0xFE) {
- $index + +;
- $ output. = $str [$index];
- }
- }
- }
- else{
- }
- $cnt + +;
- $index + +;
- }
- return $output;
- }
The above code example is the specific use of PHP substr library functions when capturing Chinese characters.
http://www.bkjia.com/PHPjc/446214.html www.bkjia.com true http://www.bkjia.com/PHPjc/446214.html techarticle Beginners The following PHP substr library function program is not perfect, but the general Chinese (GB18030,GB2312,BIG5) is not a problem. This function is not suitable for utf-8 encoded text. $str characters ...