Created
May 18, 2013 10:30
-
-
Save eb-eoliveira/5604008 to your computer and use it in GitHub Desktop.
strpos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* {{{ proto int strpos(string haystack, string needle [, int offset]) | |
Finds position of first occurrence of a string within another */ | |
PHP_FUNCTION(strpos) | |
{ | |
zval *needle; | |
char *haystack; | |
char *found = NULL; | |
char needle_char[2]; | |
long offset = 0; | |
int haystack_len; | |
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &haystack, &haystack_len, &needle, &offset) == FAILURE) { | |
return; | |
} | |
if (offset < 0 || offset > haystack_len) { | |
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset not contained in string"); | |
RETURN_FALSE; | |
} | |
if (Z_TYPE_P(needle) == IS_STRING) { | |
if (!Z_STRLEN_P(needle)) { | |
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty needle"); | |
RETURN_FALSE; | |
} | |
found = php_memnstr(haystack + offset, | |
Z_STRVAL_P(needle), | |
Z_STRLEN_P(needle), | |
haystack + haystack_len); | |
} else { | |
if (php_needle_char(needle, needle_char TSRMLS_CC) != SUCCESS) { | |
RETURN_FALSE; | |
} | |
needle_char[1] = 0; | |
found = php_memnstr(haystack + offset, | |
needle_char, | |
1, | |
haystack + haystack_len); | |
} | |
if (found) { | |
RETURN_LONG(found - haystack); | |
} else { | |
RETURN_FALSE; | |
} | |
} | |
/* }}} */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment