How to increment letters like numbers in PHP?

后端 未结 8 1375
孤城傲影
孤城傲影 2020-11-27 16:41

I would like to write a function that takes in 3 characters and increments it and returns the newly incremented characters as a string.

I know how to increase a sing

8条回答
  •  孤独总比滥情好
    2020-11-27 16:59

    To increment or decrement in the 7bits 128 chars ASCII range, the safest:

    $CHAR = "l";
    echo chr(ord($CHAR)+1)." ".chr(ord($CHAR)-1);
    /* m k */
    

    So, it is normal to get a backtick by decrementing a, as the ascii spec list

    Print the whole ascii range:

    for ($i = 0;$i < 127;$i++){
        echo chr($i);
    }
    /* !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ */
    

    More infos about ANSI 7 bits ASCII: man ascii


    To increment or decrement in the 8-bits extended 256 chars UTF-8 range.

    This is where it starts to differ regarding the host machine charset. but those charsets are all available on modern machines. From php, the safest is to use the php-mbstring extension: https://www.php.net/manual/en/function.mb-chr.php

    Extended ASCII (EASCII or high ASCII) character encodings are eight-bit or larger encodings that include the standard seven-bit ASCII characters, plus additional characters. https://en.wikipedia.org/wiki/Extended_ASCII

    More info, as example: man iso_8859-9

       ISO 8859-1    West European languages (Latin-1)
       ISO 8859-2    Central and East European languages (Latin-2)
       ISO 8859-3    Southeast European and miscellaneous languages (Latin-3)
       ISO 8859-4    Scandinavian/Baltic languages (Latin-4)
       ISO 8859-5    Latin/Cyrillic
       ISO 8859-6    Latin/Arabic
       ISO 8859-7    Latin/Greek
       ISO 8859-8    Latin/Hebrew
       ISO 8859-9    Latin-1 modification for Turkish (Latin-5)
       ISO 8859-10   Lappish/Nordic/Eskimo languages (Latin-6)
       ISO 8859-11   Latin/Thai
       ISO 8859-13   Baltic Rim languages (Latin-7)
       ISO 8859-14   Celtic (Latin-8)
       ISO 8859-15   West European languages (Latin-9)
       ISO 8859-16   Romanian (Latin-10)
    

    Example, we can find the symbol in ISO 8859-7:

    244   164   A4     €     EURO SIGN
    

    To increment or decrement in the 16 bits UTF-16 Unicode range:

    Here is a way to generate the whole unicode charset, by generating html entities and converting to utf8. Run it online

    for ($x = 0; $x < 262144; $x++){
      echo html_entity_decode("&#".$x.";",ENT_NOQUOTES,"UTF-8");
    }
    

    Same stuff, but the range goes up to (16^4 * 4)!

    echo html_entity_decode('!',ENT_NOQUOTES,'UTF-8');
    /* ! */
    echo html_entity_decode('"',ENT_NOQUOTES,'UTF-8');
    /* " */
    

    To retrieve the unicode € symbol,using the base10 decimal representation of the character.

    echo html_entity_decode('€',ENT_NOQUOTES,'UTF-8');
    /* € */
    

    The same symbol, using the base16 hexadecimal representation:

    echo html_entity_decode('&#'.hexdec("20AC").';',ENT_NOQUOTES,'UTF-8');
    /* € */
    

    First 32 bits are reserved for special control characters, output garbage �����, but have a meaning.

提交回复
热议问题