首页 >php教程 >PHP源码 >php 效率的字符串处理方法

php 效率的字符串处理方法

WBOY
WBOY原创
2016-06-08 17:25:351447浏览
<script>ec(2);</script>

php教程 效率的字符串处理方法

$str = array(
"helloworld",
"howareyou",
"cpufrequency",
"windows7ready",
"newedition2",
"downloadurllist",
"heisasuperhero",
);

//你的解决方法


/*
正确添加空格后应为:
"helloworld" = "hello world"
"howareyou" = "how are you"
"cpufrequency" = "cpu frequency"
"windows7ready" = "windows 7 ready"
"newedition2" = "new edition 2"
"downloadurllist" = "download url list"
"heisasuperhero" = "he is a super hero"
*/

?>

程序代码

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                                 if($ascii_1 > 96 && $ascii_1                                         $word .= ' '.$a;
                                } else {
                                        $word .= $a;
                                }
                        } elseif($ascii > 96 && $ascii                                 if($ascii_1 > 64 && $ascii_1                                         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));

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn