Solutions for intercepting Chinese garbled characters in PHP

Source: Internet
Author: User
Tags chr ord truncated

The simplest is to use

Example 1

The code is as follows: Copy code

<? Php
Echo substr ("Hello world! ", 6 );
?> Output:

World!

Example 2
<? Php
Echo substr ("Hello world! ", 6, 5 );
?>

There is no problem in English, but there is a problem in Chinese. Next we will use the mb_substr function for processing.

/**
*--------------------------------------
* PHP has an mbstring extension library that can be used (I was asked this question when I interviewed PHP), *
Php_mbstring.dll is not enabled on normal servers.
* Open php_mbstring.dll in php. ini. If you do not have the permission, contact your ISP.
* Because mb_string is more efficient, check whether mb_string can be used:
*--------------------------------------
*/

The code is as follows: Copy code

If (function_exists ('MB _ string '))
{
Mb_substr ($ string, $ start, $ length, $ encoding );
// Other codes here
}
Else mysubstr ($ string, $ start, $ length); // call your own function
?>

First define your own functions

The code is as follows: Copy code

<? Php
/**
*------------
* First define your own functions:
*------------
*/
Function mysubstr ($ string, $ start, $ length)
{
If (strlen ($ string)> $ length)
{
$ Str = ";
$ Len = $ start + $ length;
For ($ I = $ start; $ I <$ len; $ I ++)
{
If (ord (substr ($ string, $ I, 1)> 0xa0)
{
$ Str. = substr ($ string, $ I, 2 );
$ I ++;
}
Else {$ str. = substr ($ string, $ I, 1 );}
}
Return $ str .'... ';
}
Else {return $ string ;}
}

 

Note that when using this method, the charset in your

The following is a common Chinese/English character truncation function.

The custom function to intercept Chinese strings is basically to write a function like mb_substr and call it directly. The code I found below is actually very simple.

The code is as follows: Copy code

<? Php
Function substr_cn ($ string_input, $ start, $ length)
{
/* Function:
* This algorithm is used to intercept Chinese strings.
* The function is truncated in units of a single complete character. That is, one English character and one Chinese character both represent a unit length.
* Parameters:
* The $ string parameter is the string to be truncated,
* The $ start parameter indicates the starting position of the part to be intercepted,
* The $ length parameter indicates the number of characters to be intercepted (one Chinese character or one English character)
* Return value:
* Returns the truncated result string.
**/
$ Str_input = $ string_input;
$ Len = $ length;
$ Return_str = "";
// Define an empty string
For ($ I = 0; $ I <2 * $ len + 2; $ I ++)
$ Return_str = $ return_str ."";
$ Start_index = 0;
// Calculate the start byte offset
For ($ I = 0; $ I <$ start; $ I ++)
    {
If (ord ($ str_input {$ start_index}> = 161) // It is a Chinese character.
        {
$ Start_index + = 2;
        }
Else // English
        {
$ Start_index + = 1;
        }        
    }    
$ Chr_index = $ start_index;
// Capture
For ($ I = 0; $ I <$ len; $ I ++)
    {
$ Asc = ord ($ str_input {$ chr_index });
If ($ asc> = 161)
        {
$ Return_str {$ I} = chr ($ asc );
$ Return_str {$ I + 1} = chr (ord ($ str_input {$ chr_index + 1 }));
$ Len + = 1; // add 1 to the end condition
$ I ++; // The position Offset Plus 1
$ Chr_index + = 2;
Continue;
        }
Else
        {
$ Return_str {$ I} = chr ($ asc );
$ Chr_index + = 1;
        }
    }    
Return trim ($ return_str );
} // End of substr_cn
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.