Edit report at https://bugs.php.net/bug.php?id=78934&edit=1
ID: 78934
Updated by: cmb@php.net
Reported by: anthony_rutledge_101 at yahoo dot com
Summary: What do mbstring functions return when they fail?
-Status: Open
+Status: Not a bug
Type: Documentation Problem
Package: mbstring related
PHP Version: Irrelevant
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
> Would you please add what mb_substr() returns when it fails to
> the manual page?
It does not fail if you pass proper input; even invalid character
ranges always produce a string[1].
It only fails, if you pass unsupported input[2], in which case it
doesn't really matter what the function returns since the behavior
is undefined[3], and should never be dealt with in code. Fix the
erroneous calls instead.
[1] <https://3v4l.org/WUJhX#v730>
[2] <https://3v4l.org/sH51s>
[3] <https://www.php.net/manual/en/functions.internal.php>
Previous Comments:
------------------------------------------------------------------------
[2019-12-13 19:36:52] drtechno at mail dot com
Also, I should add and to remind everyone who uses php character filters like substr(), mbsubstr(),
htmlentities(), and htmlspecialchars() to always include the coding inline, as sometimes the
internal timing will return Null, then either the filter will not work, or will return an error.
like:
mb_substr($name 0, 7, $encoding="UTF-8");
htmlentities($name, $flags=ENT_QUOTES, $encoding="UTF-8");
or
htmlentities($name, ENT_QUOTES, "UTF-8");
------------------------------------------------------------------------
[2019-12-13 19:12:48] drtechno at mail dot com
well, it would be the same as substr() because its the same thing just with multi-byte characters
instead of the 7 bit ascii.
so the rules should be:
Returns the extracted part of string; or FALSE on failure, or an empty string.
False output example would be subtracting more than the length of the word, and it is being used in
a comparative state (ex: if(mb_substr($name, 0, 7)=="y"){ }).
------------------------------------------------------------------------
[2019-12-09 00:11:38] requinix@php.net
As with most other PHP functions, if there is some sort of problem then it returns false.
Looks like others don't have this documented either.
------------------------------------------------------------------------
[2019-12-09 00:03:53] anthony_rutledge_101 at yahoo dot com
Description:
------------
---
From manual page: https://php.net/function.mb-substr
---
Hi. Short and sweet. Would you please add what mb_substr() returns when it fails to the manual page?
Thank you. I would have given you the answer, but my PHP 7.2.7 on Windows (legacy) does not have the
multi-byte extension installed. I'm moving to PHP 7.4 anyway.
Test script:
---------------
The manual is the issue, not the function (as far as I know).
Still, to save you time. :-)
mb_internal_encoding("UTF-8");
$name = "Anthony";
echo $name . "<br>";
$newName = mb_substr($name, 0, 7);
echo $newName . "<br>";
$badName = mb_substr($name, 0, 20);
echo $badName . "<br>";
$badName = mb_substr($name, 20, 20);
echo $badName . "<br>";
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78934&edit=1