php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #40531 mb_substr optional parameters
Submitted: 2007-02-18 08:15 UTC Modified: 2012-08-01 02:00 UTC
Votes:3
Avg. Score:4.3 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:1 (33.3%)
From: emwebdev at hotmail dot com Assigned:
Status: Wont fix Package: mbstring related
PHP Version: 5.2.1 OS: Windows XP
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2007-02-18 08:15 UTC] emwebdev at hotmail dot com
Description:
------------
Hi there


I'm not sure if the following is intended functionality or a bug, but I've tried many forums etc. and been unable to get an answer.

When you use mb_substr() and omit the third argument (length), the function returns all characters from the start index to the end of the string.

This is fine unless you want to specify the character encoding explicitly as the fourth argument. This forces you to supply the length argument also.

It would seem logical that passing null for the length argument would make mb_substr() behave the same as if it had been omitted, but this is not the case.

Passing null as the length argument to mb_substr() (or substr() itself for that matter) will produce an empty string every time.

This means that to achieve the same functionality displayed when the length argument is omitted, you have to calculate the remaining string length yourself which is no doubt much less efficient.


Thanks for your time,

Erin

Reproduce code:
---------------
var_dump(substr('foobar', 3));
var_dump(substr('foobar', 3, null));

Expected result:
----------------
'bar' (length=3)
'bar' (length=3)

Actual result:
--------------
'bar' (length=3)
'' (length=0)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-02-18 08:18 UTC] emwebdev at hotmail dot com
Apologies, reproduce code should be as follows:

var_dump(mb_substr('foobar', 3));
var_dump(mb_substr('foobar', 3, null));

Expected and actual results are the same.

Thanks
 [2011-02-21 21:12 UTC] jani@php.net
-Package: Feature/Change Request +Package: mbstring related
 [2012-07-31 11:11 UTC] spam02 at live dot com
seems null = not empty in mb_substr()...
//example
$text = 'test';

mb_substr($text,-1);  //result t
mb_substr($text,-1,null) //result '' expected t
substr($text,-1); //result t
substr($text,-1, null) //result '' expected t
php version 5.3.3-7+squeeze13
 [2012-07-31 14:33 UTC] laruence@php.net
I am not sure about this,  fix will be easy, but bc break..
 [2012-08-01 02:00 UTC] aharvey@php.net
-Status: Open +Status: Wont fix
 [2012-08-01 02:00 UTC] aharvey@php.net
Documented behaviour, same reasoning as bug #62703.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 14:02:33 2014 UTC