Functions used to handle strings in mixed typesetting in PHP

Source: Internet
Author: User
Tags mixed ord strlen
/*
When we are dealing with Chinese data, we often have to deal with some situations, the following is
These situations, I do some of the functions that have been used in practice
If you have any questions, please contact me.
oicq:86804
*/
# to determine whether a location is the left or right part of a Chinese character, or not
# return value-1 left 0 not Chinese character 1 right
# usage
/*
$a = ' This is Chinese ';
Print Is_chinese ($a, 1); 0
Print Is_chinese ($a, 8); -1
Print Is_chinese ($a, 9); 1
*/
Function Is_chinese (& $str, $location) {
$ch = true;
$i = $location;
while (Ord ($str [$i]) >0xa0 && $i >= 0) {
$ch =! $ch;
$i--;
}
if ($i!= $location) {
$f _str = $ch? 1:-1;
}
else {
$f _str = false;
}
return $f _str;
}
# Chinese string inversion function
# If a string with a Chinese character is strrev upside down, it will produce garbled
/*
Print Cstrrev (' is Chinese '); The SI siht in the paper
*/
Function Cstrrev (& $str) {
$long = strlen ($STR);
for ($f _str= ', $chinese =false, $i = $long-1; $i >=0; $i-) {
if (Ord ($str [$i]) > 0xa0) {
$chinese =! $chinese;
if ($chinese = = False) {
$f _str. = $str [$i]. $str [$i +1];
}
}
else {
$f _str. = $str [$i];
}
}
return $f _str;
}
/* Chinese string intercept function
Some Chinese string intercept functions often have some problems, such as in some automatic line-wrapping programs
$a = "1 of 2";
After the interception of two times,
Csubstr ($STR, $a, 0,2);
Csubstr ($STR, $a, 2,2)
This may be the result because the loading position points to the right byte in "medium"
1, 2
Using this function will produce the correct result
1, 2
*/
# start position, starting from 0
# long = 0 is taken from start to end of string
# Ltor = True when characters are left to right, false to right to left
# $cn _len characters in bytes or words, if the number of words, then a Chinese when a byte calculation
Function Csubstr (& $str, $start =0, $long =0, $ltor =true, $cn _len=2) {
if ($long = = 0) $long = strlen ($STR);
if ($ltor = = false) $str = Cstrrev ($STR);

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.