Home  >  Article  >  Backend Development  >  详细介绍PHP中文处理函数大集结_PHP教程

详细介绍PHP中文处理函数大集结_PHP教程

WBOY
WBOYOriginal
2016-07-15 13:35:13926browse

--- PHP中文处理函数之空格 ---

string GBspace(string) --------- 每个中文字之间加空格
string GBunspace(string) ------- 每个中文字之间的空格清除
string clear_space(string) ------- 用来清除多余的空格

--- PHP中文处理函数之转换 ---

string GBcase(string,offset) --- 将字符串内的中英文字转换大小写
offset : "upper" - 字符串全转为大写 (strtoupper)
"lower" - 字符串全转为小写 (strtolower)
"ucwords" - 将字符串每个字第一个字母改大写 (ucwords)
"ucfirst" - 将字符串第一个字母改大写 (ucfirst)
string GBrev(string) ----------- 颠倒字符串

--- PHP中文处理函数之文字检查 ---

int GB_check(string) ----------- 检查字符串内是否有 GB 字,有会返回 true,
否则会返回false
int GB_all(string) ------------- 检查字符串内所有字是否有 GB 字,是会返回 true,
否则会返回false
int GB_non(string) ------------- 检查字符串内所有字并不是 GB 字,是会返回 true,
否则会返回false
int GBlen(string) -------------- 返回字符串长度(中文字只计一字母)

--- PHP中文处理函数之查找、取代、提取 ---

int/array GBpos(haystack,needle,[offset]) ---- 查找字符串 (strpos)
offset : 留空 - 查找第一个出现的位置
int - 由该位置搜索出现的第一个位置
"r" - 查找最后一次出现的位置 (strrpos)
"a" - 将所有查找到的字储存为数组(返回 array)

string GB_replace(needle,str,haystack) -- 查找与取代字符串 (str_replace)
string GB_replace_i(needle,str_f,str_b,haystack) -- 不检查大小写查找与取代字符串
needle - 查找字母
str - 取代字母 ( str_f - 该字母前, str_b 该字母后)
haystack - 字符串

string GBsubstr(string,start,[length]) -- 从string提取出由开始到结尾或长度
length的字符串。
中文字只计一字母,可使用正负数。
string GBstrnear(string,length) -- 从 string提取最接近 length的字符串。
length 中中文字计2个字母。

---PHP中文处理函数注意事项 ---

如使用由 Form 返回的字符串前,请先替字符串经过 stripslashes() 处理,除去多余的 。

用法:在原 PHP 代码内加上:
include ("GB.inc");
即可使用以上工具函数。

  1. */  
  2.  ?php  
  3. function GBlen($string) {  
  4. $l = strlen($string);  
  5. $ptr = 0;  
  6. $a = 0;  
  7. while ($a  $l) {  
  8. $ch = substr($string,$a,1);  
  9. $ch2 = substr($string,$a+1,1);  
  10. if (ord($ch) >= HexDec("0x81") 
    && ord($ch2) 
    >= HexDec("0x40")) {  
  11. $ptr++;  
  12. $a += 2;  
  13. } else {  
  14. $ptr++;  
  15. $a++;  
  16. } // END IF  
  17. } // END WHI?  
  18. ?>   


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445919.htmlTechArticle--- PHP中文处理函数之空格 --- string GBspace(string) --------- 每个中文字之间加空格 string GBunspace(string) ------- 每个中文字之间的空格清除 string...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn