使用PHP获取字母表中下一个字母的最有效方法 [英] Most efficient way to get next letter in the alphabet using PHP

查看:342
本文介绍了使用PHP获取字母表中下一个字母的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定从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?

推荐答案

认为,最有效的方法是 渐进 字符串变量.

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' 

如您所愿,递增'z'给出'aa',而想重置以获取'a',您可以简单地检查结果字符串的长度,并检查它的>1是否将其重置.

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];
}

这篇关于使用PHP获取字母表中下一个字母的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆