其他字母都大写...
我如何将字符串中的所有其他字母大写?我知道如何转换为较低或较高或第一个等,但不知道如何进行其他操作。为了清楚起见,我举了一些例子。这也是为了创建一个密码,我和我的儿子正在搞乱,我永远不会以这种方式格式化常规文本。
pizza -> PiZzA
party -> PaRtY
popcorn -> PoPcOrN
How would i capitalize every other letter in a string? I know how to convert to lower or upper or the first, etc but not sure how to go about every other. To be clear I've included examples. Also this is for creating a cipher my son and i are messing around with, i would never format regular text in this fashion.
pizza -> PiZzA
party -> PaRtY
popcorn -> PoPcOrN
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
CodePad。
CodePad.
我将尝试这样的事情:
或者,作为单行:
您可以概括该函数,将要大写的块的长度作为参数传递
并让该函数完成工作。该算法很简单:
最终功能是:
它仅使用本机构造,因此应该相当快。
编辑:
一个不使用 lambda 的函数,因此适用于 PHP 5.0 及更高版本,因为 str_split 是随 php5 版本引入的
I will try something like this:
or, as one-liner:
You can generalize the function passing as parameter the length of chunk to be capitalized
and letting the function do the work. The algorithm is straightforward:
The final function is:
It use only native constructs and thus should be quite fast.
EDIT:
A function that doesn't use lambda and therefore suitable for PHP 5.0 and onward, as str_split was introduced with the php5 release
我会将其拆分为数组,然后将其重新连接在一起:
I would split it as an array and then reattach it together: