In PHP, Ord implements Chinese string interception and solves garbled problems _php tutorial

Source: Internet
Author: User
Here are two examples of the use of Ord as an example of character ASCII to judge the problem of Chinese garbled characters, this function we can refer to the http://www.bKjia.c0m/phper/php/41030.htm article, it is very good to introduce the use of this function.

The code is as follows Copy Code

A character
$str = (Pack ("A *", "China"));
echo $str, "=", strlen ($STR), "Byte n";
Getascill ($STR);
H character
$str = (Pack ("h*", "Fffe"));
echo $str, "=", strlen ($STR), "Byte n";
Getascill ($STR);
C character
$str = (Pack ("c*", "55", "56", "57"));
echo $str, "=", strlen ($STR), "Byte n";
Getascill ($STR);

I-character short-shaped 32-bit 4-byte 64-bit 8-byte
$str = (Pack ("I", "100"));
echo $str, "=", strlen ($STR), "Byte n";
Getascill ($STR);

s character short shaping 2 bytes
$str = (Pack ("s", "100"));
echo $str, "=", strlen ($STR), "Byte n";
Getascill ($STR);

L character-length shaping 4 bytes
$str = (Pack ("L", "100"));
echo $str, "=", strlen ($STR), "Byte n";
Getascill ($STR);

F-character single-precision floating-point 4 bytes
$str = (Pack ("F", "100"));
echo $str, "=", strlen ($STR), "Byte n";
Getascill ($STR);

D-character double-precision floating-point 8 bytes
$str = (Pack ("D", "100"));
echo $str, "=", strlen ($STR), "Byte n";
Getascill ($STR);

function Getascill ($STR)
{
$arr =str_split ($STR);
foreach ($arr as $v)
{
echo $v, "=", Ord ($v), "n";
}
echo "=============RNRN";
}
?>

For more detailed information, please see: http://www.bKjia.c0m/phper/php-function/php-ord.htm

Example Two

The code is as follows Copy Code

/*
@ Another method, use the Ord () function:
@ Applies to gb2312 encoding:
*/
$str = "How will the long title of the news show only a few words in front, followed by ..." To replace it? ";
function Gb2312_substr ($STR, $limit) {
$restr = ";
for ($i =0; $i < $limit-3; $i + +) {
$restr. = Ord ($str [$i]) >127? $str [$i]. $str [+ + $i]: $str [$i];
}
return $restr;
}
/*
@ The following only applies to utf-8 encoding;
*/
function Utf8_substr ($STR, $limit) {
$restr = ";
for ($i =0; $i < $limit-3; $i + +) {
$restr. = Ord ($str [$i]) >127? $str [$i]. $str [+ + $i]. $str [+ + $i]: $str [$i];
}
return $restr;
}
Explain the first of the above: Chr (0) is not null,null is nothing, and Chr (0) is the value of 0. Expressed as 16 binary is 0x00, denoted as binary is 00000000 although Chr (0) will not show anything, but he is a character. Although Chr (0) will not show anything, but he is a character. When the Chinese character is truncated, according to the coding rules he always vlasov the other words in the back together as a Chinese character interpretation, which is the reason for garbled.
?>

http://www.bkjia.com/PHPjc/632246.html www.bkjia.com true http://www.bkjia.com/PHPjc/632246.html techarticle below we give two examples are using Ord to use the example character ASCII to judge the problem of Chinese garbled, this function we can refer to http://www.111cn.net/phper/php/4103 ...

  • 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.