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

发布于 2024-08-29 20:50:50 字数 51 浏览 3 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

楠木可依 2024-09-05 20:50:50

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

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

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

如所见,如果您不希望这样,而是想重置以获得 'a',则递增 'z' 会给出 'aa'可以简单地检查结果字符串的长度以及它的 >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];
}

The most efficient way of doing this in my opinion is to just increment the 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 >1 reset it.

$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];
}
青春有你 2024-09-05 20:50:50

这取决于您按下 Z 时想要执行的操作,但您有几个选择:

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

您还可以使用 PHP 的 range() 函数:

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

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

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

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

$chars = range('a', 'z');  // ['a', 'b', 'c', 'd', ...]
药祭#氼 2024-09-05 20:50:50

好吧,这取决于您到底想对“边缘情况”做什么。当字符为 zZ 时,您期望什么结果?您想要相同大小写的下一个字母,还是只需要下一个字母、句号?

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

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

但是当您位于 Z 时,这将为您提供 [z 将为您提供 {(根据 ASCII 值)。


根据评论进行编辑:

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

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

这些都是非常简单的操作,我真的不会担心效率像这样的一个案例。

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

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 Z this will give you [, and z will give you {, according to ASCII values.


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.

画▽骨i 2024-09-05 20:50:50

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

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

How about using ord() and chr()?

<?php
    $next = chr(ord($prev)+1);
?>
半步萧音过轻尘 2024-09-05 20:50:50
$val = 'z';
echo chr((((ord($val) - 97) + 1) % 26) + 97);

又好又简单:-)

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

Nice and easy :-)

晚风撩人 2024-09-05 20:50:50

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

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

感谢您的帮助,伙计们!

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!

向地狱狂奔 2024-09-05 20:50:50

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文