Introduces a set of Chinese processing tool functions _php

Source: Internet
Author: User
Keywords functions tools Handling Chinese intro NE
/* Chinese processing tool functions
---space---
String Gbspace (String)---------spaces between each text
String Gbunspace (String)-------white space between each text
String Clear_space (String)-------used to clear extra spaces

---conversion---
String gbcase (String,offset)---Converts the Chinese and English characters inside a string into uppercase and lowercase
Offset: "Upper"-string full capitalization (Strtoupper)
"Lower"-full string converted to lowercase (strtolower)
"Ucwords"-capitalize the first letter of each word of the string (ucwords)
"Ucfirst"-capitalizes the first letter of the string (Ucfirst)
String Gbrev (String)-----------upside down strings

---text check---
int Gb_check (string)-----------Check if there is a GB word inside the string, there will be a return of true,
Otherwise it will return false
int Gb_all (string)-------------check if all the words in the string have a GB word, it will return true.
Otherwise it will return false
int Gb_non (string)-------------check that all the words inside the string are not gigabytes, and that it returns true.
Otherwise it will return false
int Gblen (String)--------------returns the length of the string (only one letter in Chinese text)

---find, replace, extract---
Int/array Gbpos (Haystack,needle,[offset])----Find string (Strpos)
Offset: Empty-finds the first occurrence of the position
INT-the first position to be searched by the location
"R"-Find the last occurrence (Strrpos)
"A"-stores all found words in an array (returns an array)

String Gb_replace (Needle,str,haystack)--Find and replace string (str_replace)
String Gb_replace_i (Needle,str_f,str_b,haystack)--Do not check case lookup vs. replace string
Needle-Find letters
STR-Substituted letter (Str_f-Before the letter, str_b the letter)
Haystack-string

String Gbsubstr (String,start,[length])-Extracts from a string from start to end or length
The length of the string.
The Chinese text only counts one letter and can use positive negative numbers.
String Gbstrnear (string,length)-extracts the string closest to length from a string.
Length in Chinese characters 2 letters.

---note---
If you use the string returned by the Form, precede the string with stripslashes (), removing the redundant \.

To use: In the original PHP code, add:
Include ("Gb.inc");
You can use the above tool functions.
*/

function Gblen ($string) {
$l = strlen ($string);
$ptr = 0;
$a = 0;
while ($a < $l) {
$ch = substr ($string, $a, 1);
$ch 2 = substr ($string, $a +1,1);
if (Ord ($ch) >= hexdec ("0x81") && Ord ($ch 2) >= hexdec ("0x40")) {
$ptr + +;
$a + = 2;
} else {
$ptr + +;
$a + +;
}//END IF
}//END while

return $ptr;
}

function Gbsubstr ($string, $start, $length) {
if (!is_int ($length) && $length! = "") {
Return "Error: Length value error (must be numeric).
";
} elseif ($length = = "0") {
Return "";
} else {
$l = strlen ($string);
$a = 0;
$ptr = 0;
$str _list = Array ();
$str _list2 = Array ();
while ($a < $l) {
$ch = substr ($string, $a, 1);
$ch 2 = substr ($string, $a +1,1);
if (Ord ($ch) >= hexdec ("0x81") && Ord ($ch 2) >= hexdec ("0x40")) {
$str _list[$ptr] = $a;
$str _list2[$ptr] = $a +1;
$ptr + +;
$a + = 2;
} else {
$str _list[$ptr] = $a;
$str _list2[$ptr] = $a;
$ptr + +;
$a + +;
}//END IF
}//END while

if ($start > $ptr | |-$start > $PTR) {
Return
} elseif ($length = = "") {
if ($start >= 0) {//(text,+)
Return substr ($string, $str _list[$start]);
} else {//(test,-)
Return substr ($string, $str _list[$ptr + $start]);
}
} else {

if ($length > 0) {//$length > 0


if ($start >= 0) {//(text,+,+)
if ($start + $length) >= count ($str _list2)) {
Return substr ($string, $str _list[$start]);
} else {//(text,+,+)
$end = $str _list2[$start + ($length-1)]-$str _list[$start] +1;
Return substr ($string, $str _list[$start], $end);
}

} else {//(text,-, +)
$start = $ptr + $start;
if ($start + $length) >= count ($str _list2)) {
Return substr ($string, $str _list[$start]);
} else {
$end = $str _list2[$start + ($length-1)]-$str _list[$start] +1;
Return substr ($string, $str _list[$start], $end);
}
}

} else {//$length < 0
$end = strlen ($string)-$str _list[$ptr + $length];
if ($start >= 0) {//(text,+,-) {
Return substr ($string, $str _list[$start],-$end);
} else {//(text,-,-)
$start = $ptr + $start;
Return substr ($string, $str _list[$start],-$end);
}

}//END of LENGTH >/< 0

}
}//END IF
}

function Gb_replace ($needle, $string, $haystack) {
$l = strlen ($haystack);
$l 2 = strlen ($needle);
$l 3 = strlen ($string);
$news = "";
$skip = 0;
$a = 0;
while ($a < $l) {
$ch = substr ($haystack, $a, 1);
$ch 2 = substr ($haystack, $a +1,1);
if (Ord ($ch) >= hexdec ("0x81") && Ord ($ch 2) >= hexdec ("0x40")) {
if (substr ($haystack, $a, $l 2) = = $needle) {
$news. = $string;
$a + = $l 2;
} else {
$news. = $ch. $ch 2;
$a + = 2;
}
} else {
if (substr ($haystack, $a, $l 2) = = $needle) {
$news. = $string;
$a + = $l 2;
} else {
$news. = $ch;
$a + +;
}
}//END IF
}//END while
return $news;
}

function Gb_replace_i ($needle, $str _f, $str _b, $haystack) {

$l = strlen ($haystack);
$l 2 = strlen ($needle);
$l 3 = strlen ($string);
$news = "";
$skip = 0;
$a = 0;
while ($a < $l) {
$ch = substr ($haystack, $a, 1);
$ch 2 = substr ($haystack, $a +1,1);
if (Ord ($ch) >= hexdec ("0x81") && Ord ($ch 2) >= hexdec ("0x40")) {
if (Gbcase (substr ($haystack, $a, $l 2), "lower") = = Gbcase ($needle, "lower")) {
$news. = $str _f. substr ($haystack, $a, $l 2). $str _b;
$a + = $l 2;
} else {
$news. = $ch. $ch 2;
$a + = 2;
}
} else {
if (Gbcase (substr ($haystack, $a, $l 2), "lower") = = Gbcase ($needle, "lower")) {
$news. = $str _f. substr ($haystack, $a, $l 2). $str _b;
$a + = $l 2;
} else {
$news. = $ch;
$a + +;
}
}//END IF
}//END while
return $news;
}



function Gbpos ($haystack, $needle, $offset) {
if (!is_int ($offset)) {
$offset = Strtolower ($offset);
if ($offset! = "" && $offset! = "R" && $offset! = "a") {
Return "Error: Offset value error.
";
}
}
$l = strlen ($haystack);
$l 2 = strlen ($needle);
$found = false;
$w = 0; Word
$a = 0; START

if ($offset = = "" | | $offset = = "R") {
$atleast = 0;
$value = false;
} elseif ($offset = = "a") {
$value = Array ();
$atleast = 0;
} else {
$value = false;
$atleast = $offset;
}
while ($a < $l) {
$ch = substr ($haystack, $a, 1);
$ch 2 = substr ($haystack, $a +1,1);
if (Ord ($ch) >= hexdec ("0x81") && Ord ($ch 2) >= hexdec ("0x40") && $skip = = 0) {
if (substr ($haystack, $a, $l 2) = = $needle) {
if ($offset = = "R") {
$found = true;
$value = $w;
} elseif ($offset = = "a") {
$found = true;
$value [] = $w;
} elseif (! $value) {
if ($w >= $atleast) {
$found = true;
$value = $w;
}
}
}
$a + = 2;
} else {
if (substr ($haystack, $a, $l 2) = = $needle) {
if ($offset = = "R") {
$found = true;
$value = $w;
} elseif ($offset = = "a") {
$found = true;
$value [] = $w;
} elseif (! $value) {
if ($w >= $atleast) {
$found = true;
$value = $w;
}
}
}
$a + +;
}
$w + +;
}//END of while
if ($found) {
return $value;
} else {
return $false;
}
}//END of while

}

function Gbrev ($text) {
$news = "";
$l = strlen ($text);
$GB = 0;
$a = 0;
while ($a < $l) {
$ch = substr ($text, $a, 1);
$ch 2 = substr ($text, $a +1,1);
if (Ord ($ch) >= hexdec ("0x81") && Ord ($ch 2) >= hexdec ("0x40") && $skip = = 0) {
$a + = 2;
$news = $ch. $ch 2. $news;
} else {
$news = $ch. $news;
$a + +;
}
}
return $news;
}

function Gb_check ($text) {
$l = strlen ($text);
$a = 0;
while ($a < $l) {
$ch = substr ($text, $a, 1);
$ch 2 = substr ($text, $a +1,1);
if (Ord ($ch) >= hexdec ("0x81") && Ord ($ch 2) >= hexdec ("0x40")) {
return true;
} else {
return false;
}
}
}

function Gb_all ($text) {
$l = strlen ($text);
$all = 1;
$a = 0;
while ($a < $l) {
$ch = substr ($text, $a, 1);
$ch 2 = substr ($text, $a +1,1);
if (Ord ($ch) >= hexdec ("0x81") && Ord ($ch 2) >= hexdec ("0x40")) {
$a + = 2;
} else {
$a + +;
$all = 0;
}
}
if ($all = = 1) {
return true;
} else {
return false;
}
}

function Gb_non ($text) {
$l = strlen ($text);
$all = 1;
$a = 0;
while ($a < $l) {
$ch = substr ($text, $a, 1);
$ch 2 = substr ($text, $a +1,1);
if (Ord ($ch) >= hexdec ("0x81") && Ord ($ch 2) >= hexdec ("0x40")) {
$a + = 2;
$all = 0;
} else {
$a + +;
}
}
if ($all = = 1) {
return true;
} else {
return false;
}
}


function Gbcase ($text, $case) {
$case = Strtolower ($case);
if ($case! = "Upper" && $case! = "Lower" && $case! = "Ucwords" && $case! = "Ucfirst") {
Return "function usage error. $case ";
} else {
$ucfirst = 0;
$ucwords = 0;
$news = "";
$l = strlen ($text);
$GB = 0;
$english = 0;

$a = 0;
while ($a < $l) {

$ch = substr ($text, $a, 1);
if ($GB = = 0 && ord ($ch) >= hexdec ("0x81")) {

$GB = 1;
$english = 0;
$news. = $ch;
$ucwords = 0;

} elseif ($GB = = 1 && ord ($ch) >= hexdec ("0x40") && $english = = 0) {
$news. = "$ch";
$ucwords = 0;
$GB = 0;

} else {
if ($case = = "Upper") {
$news. = Strtoupper ($ch);
} elseif ($case = = "Lower") {
$news. = Strtolower ($ch);
} elseif ($case = = "Ucwords") {
if ($ucwords = = 0) {
$news. = Strtoupper ($ch);
} else {
$news. = Strtolower ($ch);
}
$ucwords = 1;
} elseif ($case = = "Ucfirst") {
if ($ucfirst = = 0) {
$news. = Strtoupper ($ch);
$ucfirst = 1;
} else {
$news. = Strtolower ($ch);
$ucfirst = 1;
}
} else {
$news. = $ch;
}
if ($ch = = "" | | $ch = = "\ n") {
$ucwords = 0;
}
$english = 1;
$GB = 0;

}

$a + +;

}//END of while
return $news;
}//End Else
}



function Gbspace ($text) {

$news = "";
$l = strlen ($text);
$GB = 0;
$english = 0;

$a = 0;
while ($a < $l) {


$ch = substr ($text, $a, 1);
$ch 2 = substr ($text, $a +1,1);
if (! ( $ch = = "" && $ch 2 = = "")) {
if ($GB = = 0) {
if (Ord ($ch) >= hexdec ("0x81")) {

if ($english = = 1) {
if ((substr ($text, $a -1,1) = = "") | | (Substr ($text, $a -1,1) = = "\ n")) {
$news. = "$ch";
} else {
$news. = "$ch";
}
$english = 0;
$GB = 1;
} else {
$GB = 1;
$english = 0;
$news. = $ch;
}
} else {
$english = 1;
$GB = 0;
$news. = $ch;
}

} else {
if (Ord ($ch) >= hexdec ("0x40")) {
if ($english = = 0) {
if ((substr ($text, $a +1,1) = = "") | | (Substr ($text, $a +1,1) = = "\ n")) {
$news. = "$ch";
} else {
$news. = "$ch";
}
} else {
$news. = "$ch";
}
} else {
$english = 1;
$news. = "$ch";
}
$GB = 0;
}
}
$a + +;
}//END of while

CHK 1 & Last is space

$l = strlen ($news);
if (substr ($news, 0, 1) = = "") {
$news = substr ($news, 1);
}
$l = strlen ($news);
if (substr ($news, $l -1,1) = = "") {
$news = substr ($news, 0, $l-1);
}
return $news;
}

function Gbunspace ($text) {
$news = "";
$l = strlen ($text);
$a = 0;
$last _space = 1;
while ($a < $l) {

$ch = substr ($text, $a, 1);
$ch 2 = substr ($text, $a +1,1);
$ch 3 = substr ($text, $a +2,1);
if (($a + 1) = = = $l) {
$last _space = 1;
}
if ($ch = = "") {
if ($last _space = = 0) {
if (Ord ($ch 2) >= hexdec ("0x81") && Ord ($ch 3) >= hexdec ("0x40")) {
if ($chi = = 0) {
$news. = "";
$last _space = 1;
}
$chi = 1;


} elseif ($ch 2! = "") {
$news. = "";
$chi = 0;
$last _space = 1;
}
}
} else {
if (Ord ($ch) >= hexdec ("0x81") && Ord ($ch 2) >= hexdec ("0x40")) {
$chi = 1;
$a + +;
$news. = $ch. $ch 2;
$last _space = 0;

} else {
$chi = 0;
$news. = $ch;
$last _space = 0;
}

}
$a + +;
}
CHK 1 & Last is space

$l = strlen ($news);
if (substr ($news, 0, 1) = = "") {
$news = substr ($news, 1);
}
$l = strlen ($news);
if (substr ($news, $l -1,1) = = "") {
$news = substr ($news, 0, $l-1);
}
return $news;



}//END of Function

function Gbstrnear ($text, $length) {

$tex _len = strlen ($text);
$a = 0;
$w = "";
while ($a < $tex _len) {
$ch = substr ($text, $a, 1);
$ch 2 = substr ($text, $a +1,1);
if (Gb_all ($ch. $ch 2)) {
$w. = $ch. $ch 2;
$a = $a +2;
} else {
$w. = $ch;
$a + +;
}
if ($a = = $length | | $a = = ($length-1)) {
$a = $tex _len;
}
}
return $w;
}//END of FUNCTION

function Clear_space ($text) {
$t = "";
for ($a =0; $a
$ch = substr ($text, $a, 1);
$ch 2 = substr ($text, $a +1,1);
if ($ch = = "" && $ch 2 = = "") {
} else {
$t. = $ch;
}
}
return $t;
}


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