Question

With substr() you can omit the third parameter to get the whole rest of the string:

substr('abcdefg', 2) // returns "cdefg"

You can't do the same with mb_substr():

mb_substr('abcdefg', 2, null, 'UTF-8'); // returns empty string

I only found weird and ugly solutions.

  • Setting a very high number as length:

    $a = mb_substr('abcdefg', 2, 9999999999, 'UTF-8');

  • Calculating the number:

    $a = mb_substr('abcdefg', 2, mb_strlen('abcdefg', 'UTF-8') - 2, 'UTF-8');

  • Omitting the charset parameter by using mb_internal_encoding():

    $temp = mb_internal_encoding(); // prevent action at a distance
    mb_internal_encoding('UTF-8');
    $a = mb_substr('abcdefg', 2);
    mb_internal_encoding($temp);

Isn't there a real solution?

Was it helpful?

Solution

Change log shows this as a bug fix in Version 5.4.8 (18-October-2012).

http://us.php.net/ChangeLog-5.php

Allow passing null as a default value to mb_substr() and mb_strcut(). Patch by Alexander Moskaliov via GitHub PR #133.

Here is a link to the pull request thread also: https://github.com/php/php-src/pull/133

OTHER TIPS

This is a difference between PHP 5.3 (and probably also in earlier versions) and PHP 5.4.

Actually, you can see the problem in the source code of PHP if you are curious.

It is the file ext/mbstring/mbstring.c which has the following difference in the PHP_FUNCTION(mb_substr) function.

In PHP 5.3 they check for this condition:

if (argc < 3) {
    len = str_len;
}

Whereas in PHP 5.4 they use:

if (argc < 3 || Z_TYPE_PP(z_len) == IS_NULL) {
    len = str_len;
}

You can find these definitions in the implementation of the mb_string function which is started by PHP_FUNCTION(mb_substr) in the mentioned file. Source code can be downloaded from php.net download page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top