>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으로 문의하세요.