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

php 效率的字符串处理方法

WBOY
WBOYOriginal
2016-06-08 17:25:351443browse
<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));

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