1

I've been looking through various php docs, but cannot find the answer to this.

If I have a string

$str = '123-456-789-...';

will the indexes of the returned array from explode() always be in order of occurrence?

So, for the string above:

explode('-', $str);

will always return:

[
  '0' : '123', 
  '1' : '456', 
  '2' : '789', 
  ...
]

?? Or is there some voodoo magic happening behind the scenes that might result in mismatched indexes? All my testing has been in the proper order, but I remember something somewhere (JS, maybe?), where indexes weren't guaranteed to come out the same as they went in.

2
  • 1
    That seems so implicit it goes without saying.
    – deceze
    Sep 1, 2017 at 21:09
  • 1
    @deceze I agree, but I've been burned by things like this before. I just wish the docs would explicitly say so.
    – Birrel
    Sep 1, 2017 at 21:10

2 Answers 2

2

As taken from the PHP source code here, the function explode goes in order of the given string from left to right.

Here is the code itself:

PHPAPI void php_explode(const zend_string *delim, zend_string *str, zval *return_value, zend_long limit){
    char *p1 = ZSTR_VAL(str);
    char *endp = ZSTR_VAL(str) + ZSTR_LEN(str);
    char *p2 = (char *) php_memnstr(ZSTR_VAL(str), ZSTR_VAL(delim), ZSTR_LEN(delim), endp);
    zval  tmp;
    if (p2 == NULL) {
        ZVAL_STR_COPY(&tmp, str);
        zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
    } else {
        do {
            size_t l = p2 - p1;
            if (l == 0) {
                ZVAL_EMPTY_STRING(&tmp);
            } else if (l == 1) {
                ZVAL_INTERNED_STR(&tmp, ZSTR_CHAR((zend_uchar)(*p1)));
            } else {
                ZVAL_STRINGL(&tmp, p1, p2 - p1);
            }
            zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
            p1 = p2 + ZSTR_LEN(delim);
            p2 = (char *) php_memnstr(p1, ZSTR_VAL(delim), ZSTR_LEN(delim), endp);
        } while (p2 != NULL && --limit > 1);
        if (p1 <= endp) {
            ZVAL_STRINGL(&tmp, p1, endp - p1);
            zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
        }
    }
}
1

Yes it is guaranteed. It performs a simple pattern match.

Example: https://eval.in/853910

I'm not sure I've ever seen something like explode in any language randomize the return.

5
  • I've done plenty of test cases that all show the same thing as your example, but can you point to any particular documentation stating so? A single example does not an answer make.
    – Birrel
    Sep 1, 2017 at 21:12
  • 1
    The source code is freely available for viewing: github.com/php/php-src/blob/master/ext/standard/string.c --I would argue that absence of contrary evidence nor examples AND viewing the source code makes it pretty clear that is exactly how the function will operate every single time it is called.
    – Mike S
    Sep 1, 2017 at 21:16
  • 1
    @neuromatter, I would rather say: How the function will operate until somebody changes it for any or no reason. The change in the list() function from PHP5 to PHP7 comes to mind. Simple Example.
    – jh1711
    Sep 1, 2017 at 21:28
  • True, but changes across major releases is a different topic. By that logic anything could change so nothing is ever guaranteed. But that doesn't really get you anywhere..? Your question implied if you continued to call the same function (in the same version since you didn't specify different versions) would it be possible to receive an unexpected index order. The answer to that is still no.
    – Mike S
    Sep 1, 2017 at 21:40
  • I'm sorry if my comment came across as offensive. I didn't mean it that way. I also don't want to get anywhere; I was reminded of one of the rare BC breaks in PHP, and decided to share it.
    – jh1711
    Sep 1, 2017 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.