douxuzui4590 2019-06-01 03:04
浏览 26

字母表作为字符串......怎么样?

I've been looking and searching for a long time now, but I can't find any way in PHP to grab a string such as "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" by telling it the input language or locale (not sure which is relevant... maybe both). In fact, I can't even find a non-PHP list of these online anywhere. Except for the USA.

Do I have to figure this out myself and then produce my own strings, or is this built into the language somehow and ready for me to "grab"?

  • 写回答

1条回答 默认 最新

  • doudou8893 2019-06-01 15:59
    关注

    If you know the first and last letter of your alphabet, it is possible to use a combination of mb_strtolower and a mb_range function I found here : GitHub rodneyrehm

    $low_letters = mb_range('α', 'ω');
    $upper_letters = array_map('mb_strtoupper', $low_letters);
    
    echo implode('', array_merge($low_letters, $upper_letters)); // αβγδεζηθικλμνξοπρςστυφχψωΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΣΤΥΦΧΨΩ
    

    And the code of the mb_range function (in case the GitHub disappear) :

     mb_internal_encoding('UTF-8');
    /**
     * multibyte string compatible range('A', 'Z')
     *
     * @param string $start Character to start from (included)
     * @param string $end Character to end with (included)
     * @return array list of characters in unicode alphabet from $start to $end
     * @author Rodney Rehm
     */
    function mb_range($start, $end) {
        // if start and end are the same, well, there's nothing to do
        if ($start == $end) {
            return array($start);
        }
    
        $_result = array();
        // get unicodes of start and end
        list(, $_start, $_end) = unpack("N*", mb_convert_encoding($start . $end, "UTF-32BE", "UTF-8"));
        // determine movement direction
        $_offset = $_start < $_end ? 1 : -1;
        $_current = $_start;
        while ($_current != $_end) {
            $_result[] = mb_convert_encoding(pack("N*", $_current), "UTF-8", "UTF-32BE");
            $_current += $_offset;
        }
        $_result[] = $end;
        return $_result;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号