当前位置:首页 > PHP教程 > php应用 > 列表

php 效率的字符串处理方法

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-05 13:56:36 浏览: 评论:0 

php效率的字符串处理方法,代码如下:

  1. <?php 
  2. $str = array
  3. "helloworld"
  4. "howareyou"
  5. "cpufrequency"
  6. "windows7ready"
  7. "newedition2"
  8. "downloadurllist"
  9. "heisasuperhero"
  10. ); 
  11. //你的解决方法 
  12. /* 
  13. 正确添加空格后应为: 
  14. "helloworld" = "hello world" 
  15. "howareyou" = "how are you" 
  16. "cpufrequency" = "cpu frequency" 
  17. "windows7ready" = "windows 7 ready" 
  18. "newedition2" = "new edition 2" 
  19. "downloadurllist" = "download url list" 
  20. "heisasuperhero" = "he is a super hero" 
  21. */ 
  22. ?> 

程序代码如下:

  1. function transfer($input) { 
  2.         $newarray = array(); 
  3.         foreach($input as $i) { 
  4.                 $arr = str_split($i); 
  5.                 $word = ''
  6.                 foreach($arr as $a) { 
  7.                         $ascii = ord($a); 
  8.                         $lastword = substr($word, -1); 
  9.                         $ascii_1 = ord($lastword); 
  10.                         $lastword_ = substr($word, -2, 1); 
  11.                         if($ascii > 64 && $ascii < 91) { 
  12.                                 if($ascii_1 > 96 && $ascii_1 < 122) { 
  13.                                         $word .= ' '.$a
  14.                                 } else { 
  15.                                         $word .= $a
  16.                                 } 
  17.                         } elseif($ascii > 96 && $ascii < 122) { 
  18.                                 if($ascii_1 > 64 && $ascii_1 < 91) { 
  19.                                         if(strlen($word) == 1) { 
  20.                                                 $word .= $a
  21.                                         } else { 
  22.                                                 if(ord($lastword_) == 32) { 
  23.                                                         $word .= $a
  24.                                                 } else { 
  25.                                                         $word = substr($word, 0, -1).' '.$lastword.$a
  26.                                                 } 
  27.                                         } 
  28.                                 } else { 
  29.                                         $word .= $a
  30.                                 } 
  31.                         } else { 
  32.                                 if(strlen($word) == 0) { 
  33.                                         $word .= $a
  34.                                 } else { 
  35.                                         $word .= ' '.$a
  36.                                 } 
  37.                         } 
  38.                 } 
  39.                 $newarray[$i] = $word
  40.         } 
  41.         return $newarray
  42.  
  43. print_r(transfer($str)); 

Tags: 字符串 效率 方法

分享到: