使用 PHP 获取字母表中下一个字母的最有效方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2673360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:18:14  来源:igfitidea点击:

Most efficient way to get next letter in the alphabet using PHP

phpstringalphabet

提问by Mathias Bynens

Given any character from a to z, what is the most efficient way to get the next letter in the alphabet using PHP?

给定从 a 到 z 的任何字符,使用 PHP 获取字母表中的下一个字母的最有效方法是什么?

回答by codaddict

The most efficient way of doing this in myopinion is to just incrementthe string variable.

看来,最有效的方法是增加字符串变量。

$str = 'a';
echo ++$str; // prints 'b'

$str = 'z';
echo ++$str; // prints 'aa' 

As seen incrementing 'z'give 'aa'if you don't want this but instead want to reset to get an 'a'you can simply check the length of the resulting string and if its >1reset it.

正如所看到的递增'z'给予'aa',如果你不希望这样,而是想重新得到一个'a'你可以简单地检查所生成的字符串的长度,如果其>1复位。

$ch = 'a';
$next_ch = ++$ch; 
if (strlen($next_ch) > 1) { // if you go beyond z or Z reset to a or A
 $next_ch = $next_ch[0];
}

回答by nickf

It depends on what you want to do when you hit Z, but you have a few options:

这取决于您点击 Z 时想做什么,但您有几个选择:

$nextChar = chr(ord($currChar) + 1); // "a" -> "b", "z" -> "{"

You could also make use of PHP's range()function:

您还可以使用 PHP 的range()功能:

$chars = range('a', 'z');  // ['a', 'b', 'c', 'd', ...]

回答by Chad Birch

Well, it depends what exactly you want to do with the "edge cases". What result do you expect when the character is zor Z? Do you want the next letter of the same case, or just the next letter, period?

好吧,这取决于您究竟想对“边缘情况”做什么。当角色是zor时,你期望什么结果Z?你想要同一个 case的下一个字母,还是只是下一个字母,句号?

Without knowing the answer to that, for the very basic case, you can just do this:

在不知道答案的情况下,对于非常基本的情况,您可以这样做:

$next_character = chr(ord($current_character) + 1);

But when you're at Zthis will give you [, and zwill give you {, according to ASCII values.

但是,当您在Z此时[z{根据 ASCII 值给您并且会给您。



Edited as per comment:

根据评论编辑:

If you need the next character of the same case, you can probably just add simple checks after the line above:

如果您需要相同大小写的下一个字符,您可以在上面的行之后添加简单的检查:

if ($next_character == '[')
    $next_character = 'A';
else if ($next_character == '{')
    $next_character = 'a';

These are very simple operations, I really wouldn't worry about efficiency in a case like this.

这些都是非常简单的操作,在这种情况下我真的不会担心效率。

回答by Anax

How about using ord() and chr()?

使用ord() 和 chr()怎么样?

<?php
    $next = chr(ord($prev)+1);
?>

回答by Mathias Bynens

Since I only care about lowercase characters in this case, I'll use the following code, based on the answers posted here:

由于在这种情况下我只关心小写字符,因此我将根据此处发布的答案使用以下代码:

function nextLetter(&$str) {
 $str = ('z' === $str ? 'a' : ++$str);
}

Thanks for the help, guys!

感谢您的帮助,伙计们!

回答by DoXicK

$val = 'z';
echo chr((((ord($val) - 97) + 1) % 26) + 97);

Nice and easy :-)

好,易于 :-)

回答by user325894

Create an array of all letters, search for existing letter and return its next letter. If you reach the last letter return first letter.

创建一个包含所有字母的数组,搜索现有字母并返回其下一个字母。如果到达最后一个字母,则返回第一个字母。