php 效率的字符串处理方法
php效率的字符串处理方法,代码如下:
<?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" */ ?>
程序代码如下:
<?php
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));
?>文章地址:http://www.phprm.com/develop/fs4203.html
转载随意^^请带上本文地址!