BeginnerThe following PHP Substr library function program is not perfect, but it is no problem to process General Chinese (GB18030, GB2312, BIG5. 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 preceding code example shows how to use the PHP Substr library function to intercept Chinese characters.