PHP Chinese processing tool function
--- space ---
string GBspace(string) --------- Add spaces between each Chinese character
string GBunspace(string) ------- Clear the space between each Chinese character
string clear_space(string) ------- Used to clear excess spaces
---Convert---
string GBcase(string,offset) --- Convert Chinese and English characters in the string to uppercase and lowercase
offset: "upper" - Convert the string to uppercase (strtoupper)
"lower" - Convert the string to all lowercase (strtolower)
"ucwords" - Capitalize the first letter of each word in a string (ucwords)
"ucfirst" - Capitalize the first letter of the string (ucfirst)
string GBrev(string) ---------- Reverse a string
---Text check---
int GB_check(string) ----------- Check whether there is GB word in the string. If so, it will return true.
Otherwise it will return false
int GB_all(string) ------------- Check whether all words in the string have GB words, if so, it will return true.
Otherwise it will return false
int GB_non(string) ------------- Check if all the words in the string are not GB words, it will return true.
Otherwise it will return false
int GBlen(string) -------------- Returns the length of the string (Chinese characters only count one letter)
---Find, replace, extract---
int/array GBpos(haystack,needle,[offset]) ---- Search string (strpos)
offset : Leave blank - find the first occurrence
int - the first position to be searched for by this position
"r" - Find the last occurrence of the position (strrpos)
"a" - stores all found words as an array (returns array)
string GB_replace(needle,str,haystack) -- find and replace string (str_replace)
string GB_replace_i(needle,str_f,str_b,haystack) -- search and replace strings without checking case
needle - find letters
str - the replacement letter (str_f - before the letter, str_b after the letter)
haystack - String
string GBsubstr(string,start,[length]) -- extract the length from start to end or from string
length string.
Chinese characters only count one letter, and positive and negative numbers can be used.
string GBstrnear(string,length) -- Extract the string closest to length from string.
The Chinese characters in length include 2 letters.
--- Notice---
Before using the string returned by Form, please process the string with stripslashes() to remove redundant .
Usage: Add: to the original PHP code:
include ("GB.inc");
You can use the above tool functions.
*/
Copy PHP content to clipboard
<?php
functionGBlen($string) {
$l = strlen($string);
$ptr = 0;
$a = 0;
while ($a < $l) {
$ch = substr($string,$a,1);
$ch2 = substr($string,$a+1,1);
if (ord($ch) >= HexDec("0x81") && ord($ch2) >= HexDec("0x40")) {
$ptr++;
$a += 2;
} else {
$ptr++;
$a++;
} // END IF
} // END WHI?
?>