PHP common functions for Chinese and English mixed typesetting strings

Source: Internet
Author: User
Tags ord
# Determine if a location is the left or right half of Chinese characters, 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 (' This is Chinese ');  In the text SI siht */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 interception functions often have some problems, such as in some automatic line-wrapping Program $a = "1 in 2", after two interception, Csubstr ($STR, $a, 0,2); Csubstr ($STR, $a, 2,2) because the pickup position points to the right byte of "medium", this may be the result 1, 2 uses this function to produce the correct result 1, 2 */# start position, starting from 0 # long = 0 from start Takes the character from left to right at the end of string # Ltor = TrueFalse to the right to the left to take the character # $cn _len characters in bytes or words taken, if the number of words taken, then a Chinese when a byte calculation function csubstr (& $str, $start =0, $long =0, $ltor =true, $c  n_len=2) {if ($long = = 0) $long = strlen ($STR);  if ($ltor = = false) $str = Cstrrev ($STR);  if ($cn _len = = 1) {for ($i =0, $fs =0; $i < $start; $fs + +) $i + = (ord ($str [$fs]) <= 0xa0)? 1:0.5; For ($i =0, $fe = $fs, $i < $long, $fe + +) $i + = (ord ($str [$fe]) <= 0xa0)?  1:0.5;  $long = $fe-$fs;  } else {$fs = (Is_chinese ($str, $start) = = 1)? $start-1: $start;  $fe = $long + $start-1; $end = (Is_chinese ($str, $fe) = =-1)?  $fe-1: $fe;  $long = $end-$fs + 1;  } $f _str = substr ($str, $fs, $long);  if ($ltor = = False) $f _str = Cstrrev ($f _str);  return $f _str; # take the left string # when Cn_len = = 2 o'clock $long Take the left number of words, the inverse is to take the left number of bytes function cleft (& $str, $long, $cn _len=2) {$f _str = csubstr ($s  TR, 0, $long, True, $CN _len);  return $f _str;  } # Take Right string function Cright (& $str, $long, $cn _len=2) {$f _str = Cstrrev ($STR); $f _str = csubstr ($f _str, 0, $long, True, $CN _len);  $f _str = Cstrrev ($f _str);  return $f _str;  # to format an article with Chinese characters # No more problems arising from the line-breaking problem!!! # Note: Each line of the article must be separated by N (Chr (13)) # $width the number of characters per line # $BR What Word acts Terminator function ctext_wrap for each line (& $text, $width =60, $br = "<b  R> ") {$lines = explode (" n ", $text);  $rows = count ($lines);  for ($i =0; $i < $rows; $i + +) {$len = strlen ($lines [$i]);  for ($j =0; $j < $len; $j + = $width) {$p = $j + $width-1;  $k = 0;  if ($p < $len) {while (!is_chinese ($lines [$i], $p) && $lines [$i] [$p]! = ' && $p > $j) {$k + +;  $p--;  if ($p = = $j) $k = 0; } $f _str. = Csubstr ($lines [$i], $j, $width-$k).  $BR;  $j-= $k;  }} return $f _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.