PHP - iterate on string characters

前端 未结 9 851
失恋的感觉
失恋的感觉 2020-12-04 14:01

Is there a nice way to iterate on the characters of a string? I\'d like to be able to do foreach, array_map, array_walk, array_

相关标签:
9条回答
  • 2020-12-04 14:21
    // Unicode Codepoint Escape Syntax in PHP 7.0
    $str = "cat!\u{1F431}";
    
    // IIFE (Immediately Invoked Function Expression) in PHP 7.0
    $gen = (function(string $str) {
        for ($i = 0, $len = mb_strlen($str); $i < $len; ++$i) {
            yield mb_substr($str, $i, 1);
        }
    })($str);
    
    var_dump(
        true === $gen instanceof Traversable,
        // PHP 7.1
        true === is_iterable($gen)
    );
    
    foreach ($gen as $char) {
        echo $char, PHP_EOL;
    }
    
    0 讨论(0)
  • 2020-12-04 14:23

    Expanded from @SeaBrightSystems answer, you could try this:

    $s1 = "textasstringwoohoo";
    $arr = str_split($s1); //$arr now has character array
    
    0 讨论(0)
  • 2020-12-04 14:30

    Hmm... There's no need to complicate things. The basics work great always.

        $string = 'abcdef';
        $len = strlen( $string );
        $x = 0;
    

    Forward Direction:

    while ( $len > $x ) echo $string[ $x++ ];
    

    Outputs: abcdef

    Reverse Direction:

    while ( $len ) echo $string[ --$len ];
    

    Outputs: fedcba

    0 讨论(0)
  • 2020-12-04 14:33

    For those who are looking for the fastest way to iterate over strings in php, Ive prepared a benchmark testing.
    The first method in which you access string characters directly by specifying its position in brackets and treating string like an array:

    $string = "a sample string for testing";
    $char = $string[4] // equals to m
    

    I myself thought the latter is the fastest method, but I was wrong.
    As with the second method (which is used in the accepted answer):

    $string = "a sample string for testing";
    $string = str_split($string);
    $char = $string[4] // equals to m
    

    This method is going to be faster cause we are using a real array and not assuming one to be an array.

    Calling the last line of each of the above methods for 1000000 times lead to these benchmarking results:

    Using string[i]
    0.24960017204285 Seconds

    Using str_split
    0.18720006942749 Seconds

    Which means the second method is way faster.

    0 讨论(0)
  • 2020-12-04 14:37

    Most of the answers forgot about non English characters !!!

    strlen counts BYTES, not characters, that is why it is and it's sibling functions works fine with English characters, because English characters are stored in 1 byte in both UTF-8 and ASCII encodings, you need to use the multibyte string functions mb_*

    This will work with any character encoded in UTF-8

    // 8 characters in 12 bytes
    $string = "abcdأبتث";
    
    $charsCount = mb_strlen($string, 'UTF-8');
    for($i = 0; $i < $charsCount; $i++){
        $char = mb_substr($string, $i, 1, 'UTF-8');
        var_dump($char);
    }
    

    This outputs

    string(1) "a"
    string(1) "b"
    string(1) "c"
    string(1) "d"
    string(2) "أ"
    string(2) "ب"
    string(2) "ت"
    string(2) "ث"
    
    0 讨论(0)
  • 2020-12-04 14:38

    You can also just access $s1 like an array, if you only need to access it:

    $s1 = "hello world";
    echo $s1[0]; // -> h
    
    0 讨论(0)
提交回复
热议问题