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时间和日期函数怎么操作
php函数的多样化和功能相近真是一大特点,同一个应用可以用多种函数实现,这恐怕也是开源的一个弊端了;通过对PHP时间函数的系统学习,充分理解了时间戳和时区的概念(这个真的很重要),有需要的小伙伴可以参考下。PHP中所有函数都是UNIX纪元的,即从1970年1月1日开始的。日...
-
PHP框架的概念
PHP框架为各种技术等级的开发者减少了重复编码的需求、加快了开发进程,并在创建web应用时确保正确的编码。下面是小编分享的PHP框架的概念,欢迎大家参考!PHP成为世界上最流行的脚本语言有许多原因:灵活性,易用性等等。但通常只用PHP或者其他语言编码就会显得单调、...
-
对PHP语言认识上要避免10大误区
PHP是一种非常流行的开源服务器端脚本语言,你在万维网看到的大多数网站都是使用php开发的。但是,你大概很奇怪的注意到有少部分的人发誓要离php远远的。但是令人更奇怪的是或者很震惊的说他们不用php并不是因为一些被证实的语言缺点。他们决定不用php,是因为误解...
-
php爬虫程序中怎么样伪造ip地址防止被封
php爬虫程序中怎么样伪造ip地址防止被封是很多程序员都关心的问题。下面是小编为大家带来的'php爬虫程序中怎么样伪造ip地址防止被封的知识,欢迎阅读。代码:代码如下$ch=curl_init();curl_setopt($ch,CURLOPT_URL,"http://localhost/");curl_setopt($ch,CURLOPT...