Hi,
Even though this is documented, it is strange behaviour: If the from
parameter to substr() is at, or past, the end of the input string, the
function returns false.
The function is documented as
string substr ( string string, int start [, int length])
IMHO substr() should either
1) be changed to do like Perl, which returns an error only if start is
_beyond_ the end (not _at_):
(from http://www.perldoc.com/perl5.6/pod/func/substr.html)
my $null = substr $name, 6, 2; # returns '' (no warning)
my $oops = substr $name, 7; # returns undef, with warning
2) be changed to return an empty string, if start is either at or beyond
the end of the input string.
I have attached patches for both solutions.
If you don't change it to be like Perl, I think it should be noted in
the documentation (as with chop()).
If, on the other hand, it _is_ changed to behave like Perl, I think it
should be emphasized that you are not guaranteed that the result is a
string, because this will cause (eg. has caused us) problems when using
the result as part of the $data array to PEAR DB::execute().
Happy hacking,
Morten
--
Morten Poulsen <[EMAIL PROTECTED]>
http://www.afdelingp.dk/
--- php-4.3.4-orig/ext/standard/string.c Mon Sep 29 04:23:52 2003
+++ php-4.3.4/ext/standard/string.c Wed Nov 19 14:18:08 2003
@@ -1679,7 +1679,11 @@
}
}
- if (f >= Z_STRLEN_PP(str)) {
+ /* if "from" position is beyond the end of the string, emit a warning
+ * and return false
+ */
+ if (f > Z_STRLEN_PP(str)) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Second argument has to be
less than or equal the length of the first argument");
RETURN_FALSE;
}
--- php-4.3.4-orig/ext/standard/string.c Mon Sep 29 04:23:52 2003
+++ php-4.3.4/ext/standard/string.c Wed Nov 19 12:14:41 2003
@@ -1679,8 +1679,11 @@
}
}
+ /* if "from" position is past the end of the string, return an empty
+ * string
+ */
if (f >= Z_STRLEN_PP(str)) {
- RETURN_FALSE;
+ RETURN_STRING("", 1);
}
if ((f + l) > Z_STRLEN_PP(str)) {
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php