PHP字符串如何实现word末字符实现大小写互换
PHP字符串如何实现word末字符实现大小写互换?希望下面的内容对你有所帮助,更多详情请关注应届毕业生考试网。
一、要求:
给出一个字符串如 “A journey of, a thousand 'miles' must can't "begin" with a single step.” ,通过 PHP 程序处理变成 “a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP.”
这里需要注意:
1、每个单词最后的.字符如果是大写就变成小写,如果是小写就变成大写。
2、需要考虑类似 can't 这种形式的转换。
3、标点符号(只考虑 , ' " . ;)不用变化。
二、参考算法如下:
代码如下:
<?php
function convertLastChar($str) {
$markArr = array(", ", "' ", "" ", ". ", "; ");
$ret = "";
for ($i = 0, $j = strlen($str); $i < $j; $i++) {
if ($i < $j - 2) {
$afterStr = $str{$i + 1} . $str{$i + 2};
} else if ($i < $j - 1) {
$afterStr = $str{$i + 1} . " ";
}
if (in_array($afterStr, $markArr)
|| $i == $j - 1
|| $str{$i + 1} == " ") {
$ret .= strtoupper($str{$i}) === $str{$i}
? strtolower($str{$i})
: strtoupper($str{$i});
} else {
$ret .= $str{$i};
}
}
return $ret;
}
?>
测试代码如下:
代码如下:
<?php
//test
$str1 = "A journey of, a thousand 'miles' must can't "begin" with a single step.";
$str2 = "A journey of, a thousand 'miles' must can't "begin" with a single step. ";
$str3 = "A journey of, a thousand 'miles' must can't "begin" with a single step. a ";
$str4 = "A journey of, a thousand 'miles' must can't "begin" with a single step. a B";
$str5 = "A journey of, a thousand 'miles' must can't "begin" with a single step. a b'";
$str6 = "A journey of, a thousand 'miles' must can't "begin" with a single step. a B"";
echo "source:<br/>" . $str1 . "<br/>result:<br/>" . convertLastChar($str1) . "<br/><br/>";
echo "source:<br/>" . $str2 . "<br/>result:<br/>" . convertLastChar($str2) . "<br/><br/>";
echo "source:<br/>" . $str3 . "<br/>result:<br/>" . convertLastChar($str3) . "<br/><br/>";
echo "source:<br/>" . $str4 . "<br/>result:<br/>" . convertLastChar($str4) . "<br/><br/>";
echo "source:<br/>" . $str5 . "<br/>result:<br/>" . convertLastChar($str5) . "<br/><br/>";
echo "source:<br/>" . $str6 . "<br/>result:<br/>" . convertLastChar($str6) . "<br/><br/>";
?>
运行结果如下:
代码如下:
source:
A journey of, a thousand 'miles' must can't "begin" with a single step.
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP.
source:
A journey of, a thousand 'miles' must can't "begin" with a single step.
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP.
source:
A journey of, a thousand 'miles' must can't "begin" with a single step. a
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. A
source:
A journey of, a thousand 'miles' must can't "begin" with a single step. a B
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. A b
source:
A journey of, a thousand 'miles' must can't "begin" with a single step. a b'
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. A B'
source:
A journey of, a thousand 'miles' must can't "begin" with a single step. a B"
result:
a journeY oF, A thousanD 'mileS' musT can'T "begiN" witH A singlE steP. A b"
-
在PHP中使用随机数的三个步骤
如何做一个随机的出现的网页,可是用rand()就是不行,听说要初始化随机数,可是怎么初始化,请不吝赐教,谢谢!//第一步:初始化种子$seedarray=microtime();$seedstr=split("",$seedarray,5);$seed=$seedstr[0]*10000;//第二步:使用种子初始化随机数发生器srand($seed);//...
-
如何写出高质量的PHP程序
现在的PHP程序员都在追求所谓的高性能,但高性能并不代表是高质量,什么是高质量的PHP程序呢?我认为,高质量的PHP程序首先得具备以下的要素,就跟随本站小编一起去了解下吧,想了解更多相关信息请持续关注我们应届毕业生考试网!一、安全无论程序写这是一个很老的话题了,...
-
PHP中Http协议post请求参数
本文给大家介绍PHP中Http协议post请求参数,具体内容如下所示,就跟随本站小编一起去了解下吧,想了解更多相关信息请持续关注我们应届毕业生考试网!WEB开发中信息基本全是在POST与GET请求与响应中进行,GET因其基于URL的直观,易被我们了解,可POST请求因其信息的隐蔽,在...
-
php环境怎么配置
使用php集成环境可以轻松实现php一键安装,搭建php开发环境;phpnow,xampp,phpstudy是Windows平台上最常用的三个php集成开发环境。以下是小编为大家搜索整理的PHP环境配置的方法,欢迎大家阅读!更多精彩内容请及时关注我们考试网!运行环境配置:php54+Apache2.2+Mysq...