Created
May 18, 2013 10:31
-
-
Save eb-eoliveira/5604009 to your computer and use it in GitHub Desktop.
strstr
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 string strstr(string haystack, string needle[, bool part]) | |
Finds first occurrence of a string within another */ | |
PHP_FUNCTION(strstr) | |
{ | |
zval *needle; | |
char *haystack; | |
int haystack_len; | |
char *found = NULL; | |
char needle_char[2]; | |
long found_offset; | |
zend_bool part = 0; | |
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|b", &haystack, &haystack_len, &needle, &part) == FAILURE) { | |
return; | |
} | |
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, 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, needle_char, 1, haystack + haystack_len); | |
} | |
if (found) { | |
found_offset = found - haystack; | |
if (part) { | |
RETURN_STRINGL(haystack, found_offset, 1); | |
} else { | |
RETURN_STRINGL(found, haystack_len - found_offset, 1); | |
} | |
} | |
RETURN_FALSE; | |
} | |
/* }}} */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment