反转字符串中所有字母的大小写(大写转小写和小写转大写)
如何交换/切换字符串中字符的大小写,例如:
$str = "Hello, My Name is Tom";
运行代码后,我得到如下结果:
$newstr = "hELLO, mY nAME Is tOM";
这可能吗?
How can I swap around / toggle the case of the characters in a string, for example:
$str = "Hello, My Name is Tom";
After I run the code I get a result like this:
$newstr = "hELLO, mY nAME Is tOM";
Is this even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您的字符串仅为 ASCII,则可以使用 XOR:
输出:
If your string is ASCII only, you can use XOR:
Outputs:
好吧,我知道你已经得到了答案,但是有点晦涩的 strtr() 函数迫切需要用于此目的;)
OK I know you've already got an answer, but the somewhat obscure strtr() function is crying out to be used for this ;)
最快的方法是使用位掩码。没有笨重的字符串函数或正则表达式。 PHP 是 C 的包装器,因此如果您知道 OR、NOT、AND、XOR、NAND 等逻辑函数,我们可以很容易地操作位。:
这就是改变它的原因:
我们取
中的第 N 个字符$string
并执行异或 (^),告诉解释器获取$char
的整数值,并将第 6 位 (32) 从 1 交换为 0 或 0 交换为 1。 ASCII 字符与其对应字符相差 32(正因为如此,ASCII 是一个巧妙的设计。由于 32 是 2 的幂 (2^5),因此很容易移位。要获取字母的 ASCII 值,请使用内置的PHP 函数
ord()
:因此,您使用
strlen()
作为for
循环的中间部分来循环字符串,它将循环恰好是字符串包含字母的次数。如果位置$i
的字符是字母(az (65-90) 或 AZ (97-122)),它将将该字符交换为字母。使用位掩码的大写或小写对应部分如下是位掩码的工作原理:
我们可以反转它:
不需要
str_replace
或preg_replace
,我们只需交换位即可加或减 32根据字符的 ASCII 值,我们交换大小写。第 6 位(右起第 6 位)确定字符是大写还是小写。如果是 0,则为小写;如果为大写,则为 1。将位从 0 更改为 1 和 32,获取大写的chr()
值,然后从 1 更改为 0 减去 32,将大写字母变为小写。我们还可以有一个函数来交换特定字符的大小写:
The quickest way is with a bitmask. No clunky string functions or regex. PHP is a wrapper for C, so we can manipulate bits quite easily if you know your logical function like OR, NOT, AND, XOR, NAND, etc..:
This is what changes it:
We take the Nth character in
$string
and perform an XOR (^) telling the interpreter to take the integer value of$char
and swapping the 6th bit (32) from a 1 to 0 or 0 to 1.All ASCII characters are 32 away from their counterparts (ASCII was an ingenious design because of this. Since 32 is a power of 2 (2^5), it's easy to shift bits. To get the ASCII value of a letter, use the built in PHP function
ord()
:So you loop through the string using
strlen()
as the middle part of thefor
loop, and it will loop exactly the number of times as your string has letters. If the character at position$i
is a letter (a-z (65-90) or A-Z (97-122)), it will swap that character for the uppercase or lowercase counterpart using a bitmask.Here's how the bitmask works:
We can reverse it:
No need for
str_replace
orpreg_replace
, we just swap bits to add or subtract 32 from the ASCII value of the character and we swap cases. The 6th bit (6th from the right) determines if the character is uppercase or lowercase. If it's a 0, it's lowercase and 1 if uppercase. Changing the bit from a 0 to a 1 ads 32, getting the uppercasechr()
value, and changing from a 1 to a 0 subtracts 32, turning an uppercase letter lowercase.We can also have a function that swaps the case on a particular character:
功能与马克的答案非常相似。
@xtempore 的解释:
Very similar in function to the answer by Mark.
Explanation by @xtempore:
您需要遍历字符串测试每个字符的大小写,根据需要调用
strtolower()
或strtoupper()
,将修改后的字符添加到新字符串中。You'll need to iterate through the string testing the case of each character, calling
strtolower()
orstrtoupper()
as appropriate, adding the modified character to a new string.我知道这个问题已经很老了 - 但这是我的两种多字节实现风格。
多功能版本:
(mb_str_split 函数在此处找到):
单函数版本:
I know this question is old - but here's my 2 flavours of a multi-byte implementation.
Multi function version:
(mb_str_split function found here):
Single function version:
以下脚本支持 UTF-8 字符,如“ą”等。
PHP 7.1+
PHP 7.4+
$before
: aaAAąAŚĆżź$after
: AAaaĄaśćŻŹFollowing script supports UTF-8 characters like "ą" etc.
PHP 7.1+
PHP 7.4+
$before
: aaAAąAŚĆżź$after
: AAaaĄaśćŻŹ我想一个解决方案可能是使用这样的东西:
这让你:
即你:
问题是这将可能不能很好地处理像重音这样的特殊字符:-(
这里有一个快速建议,可能(或可能不)适用于其他一些角色:
现在的一个想法是使用
mb_strtolower
和mb_strtoupper
:它可能有助于处理特殊字符和多字节编码...I suppose a solution might be to use something like this :
Which gets you :
i.e. you :
The problem being this will probably not work nicely with special character like accents :-(
And here is a quick proposal that might (or might not) work for some other characters :
An idea, now, would be to use
mb_strtolower
andmb_strtoupper
: it might help with special characters, and multi-byte encodings...对于多字节/unicode 安全的解决方案,我可能建议根据哪个捕获组包含一个字母来改变/切换每个字母的大小写。这样,您就不必在将字母与正则表达式匹配后进行多字节基础检查。
代码:(演示)
请参阅这个答案关于如何匹配可能是多字节的字母。
For a multibyte/unicode-safe solution, I'd probably recommend mutating/toggling the case of each letter based on which capture group contains a letter. This way you don't have to make a multibyte-base check after matching a letter with regex.
Code: (Demo)
See this answer about how to match letters that might be multibyte.