php 效率的字符串处理方法 |
时间:2015-01-23 来源:西部数据 作者:西部数据 |
|
php效率的字符串处理方法,代码如下:
- <?php
- $str = array(
- "helloworld",
- "howareyou",
- "cpufrequency",
- "windows7ready",
- "newedition2",
- "downloadurllist",
- "heisasuperhero",
- );
-
-
-
-
-
-
-
-
-
-
-
- ?>
程序代码如下:
- function transfer($input) {
- $newarray = array();
- foreach($input as $i) {
- $arr = str_split($i);
- $word = '';
- foreach($arr as $a) {
- $ascii = ord($a);
- $lastword = substr($word, -1);
- $ascii_1 = ord($lastword);
- $lastword_ = substr($word, -2, 1);
- if($ascii > 64 && $ascii < 91) {
- if($ascii_1 > 96 && $ascii_1 < 122) {
- $word .= ' '.$a;
- } else {
- $word .= $a;
- }
- } elseif($ascii > 96 && $ascii < 122) {
- if($ascii_1 > 64 && $ascii_1 < 91) {
- if(strlen($word) == 1) {
- $word .= $a;
- } else {
- if(ord($lastword_) == 32) {
- $word .= $a;
- } else {
- $word = substr($word, 0, -1).' '.$lastword.$a;
- }
- }
- } else {
- $word .= $a;
- }
- } else {
- if(strlen($word) == 0) {
- $word .= $a;
- } else {
- $word .= ' '.$a;
- }
- }
- }
- $newarray[$i] = $word;
- }
- return $newarray;
- }
-
- print_r(transfer($str));
|
|
|
|