1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 08:12:21 +01:00

Added offset & length parameters to substr_count() function.

This commit is contained in:
Ilia Alshanetsky
2005-06-18 18:23:12 +00:00
parent 20cc9e3939
commit 67ea97dfdf
3 changed files with 27 additions and 3 deletions

1
NEWS
View File

@@ -8,6 +8,7 @@ PHP NEWS
(Derick)
- Added bindto socket context option. (Ilia)
- Added offset parameter to the stream_copy_to_stream() function. (Ilia)
- Added offset & length parameters to substr_count() function. (Ilia)
- Fixed PDO shutdown problem (possible inifite loop running rollback on
shutdown). (Wez)
- Fixed PECL bug #3714 (beginTransaction doesn't work if you're in

View File

@@ -4420,15 +4420,16 @@ PHP_FUNCTION(strnatcasecmp)
}
/* }}} */
/* {{{ proto int substr_count(string haystack, string needle)
/* {{{ proto int substr_count(string haystack, string needle [, int offset [, int length]])
Returns the number of times a substring occurs in the string */
PHP_FUNCTION(substr_count)
{
zval **haystack, **needle;
zval **haystack, **needle, **offset, **length;
int ac = ZEND_NUM_ARGS();
int count = 0;
char *p, *endp, cmp;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &haystack, &needle) == FAILURE) {
if (ac < 2 || ac > 4 || zend_get_parameters_ex(ac, &haystack, &needle, &offset, &length) == FAILURE) {
WRONG_PARAM_COUNT;
}
@@ -4443,6 +4444,23 @@ PHP_FUNCTION(substr_count)
p = Z_STRVAL_PP(haystack);
endp = p + Z_STRLEN_PP(haystack);
if (ac > 2) {
convert_to_long_ex(offset);
p += Z_LVAL_PP(offset);
if (p > endp) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset value %ld exceeds string length.", Z_LVAL_PP(offset));
RETURN_FALSE;
}
if (ac == 4) {
convert_to_long_ex(length);
if ((p + Z_LVAL_PP(length)) > endp) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length value %ld exceeds string length.", Z_LVAL_PP(length));
RETURN_FALSE;
}
endp = p + Z_LVAL_PP(length);
}
}
if (Z_STRLEN_PP(needle) == 1) {
cmp = Z_STRVAL_PP(needle)[0];

View File

@@ -13,6 +13,9 @@ substr_count() function
$a = str_repeat("abcacbabca", 100);
var_dump(@substr_count($a, "bca"));
var_dump(substr_count($a, "bca", 200));
var_dump(substr_count($a, "bca", 200, 50));
?>
--EXPECT--
bool(false)
@@ -22,3 +25,5 @@ int(0)
int(0)
int(100)
int(200)
int(160)
int(10)