±¾ÎÄΪ´o¼Ò*ÖÎoÁË PHPÖÐstrpos¡¢strstrºÍstripos¡¢stristrº¯Êý£¬¹(C)´o¼Ò²Î¿¼£¬¾ßÌaÄÚÈÝÈçÏÂ
strpos
mixed strpos ( string $haystack, mixed $needle [, int $offset = 0 ] )
Èç¹uoffsetÖ¸¶¨ÁË£¬²eÕÒ»a´ÓoffsetµÄλÖÿªÊ¼¡£offset²»ÄÜΪ¸ºÊý¡£
µ»ØneedleµÚÒ»´Î³oÏÖÔÚhaystackµÄλÖá£Èç¹uÔÚhaystackÖÐÕÒ²»µ½needle£¬Ôoµ»ØFALSE¡£
needle£¬Èç¹uneedle²»ÊÇ×Öu´®£¬Ëu»a±»×ª»»³ÉÕuÐÍÊýÖµ²¢¸³ÖµÎª¸ÃÊýÖµµÄASCII×Öu¡£Çe¿´ÏÂÃaeÀý×Ó¡£
Àý×Ó
$str = "hello";
$pos = strpos($str, 111);
// 111µÄASCIIÖµÊÇo£¬Òo´Ë$pos = 4
strposºËÐÄÔ´Âe
if (Z_TYPE_P(needle) == IS_STRING) {
if (!Z_STRLEN_P(needle)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty needle");
RETURN_FALSE;
}
// µ÷ÓÃphp_memnstrº¯Êý²eÕÒneedle
found = php_memnstr(haystack + offset,
Z_STRVAL_P(needle),
Z_STRLEN_P(needle),
haystack + haystack_len);
} else {
// Èç¹u²»ÊÇ×Ö*u´®£¬×ª»»³ÉÊý×Ö²¢¸³ÖµÎª¸ÃÊý×ÖµÄASCII×Ö*u¡£
if (php_needle_char(needle, needle_char TSRMLS_CC) != SUCCESS) {
RETURN_FALSE;
}
//ÉeÖýaÊø×Ö*u
needle_char[1] = 0;
found = php_memnstr(haystack + offset,
needle_char,
1,
haystack + haystack_len);
¡¡¡¡ }
}
ÓÐÒ»µaҪעÒaµÄÊÇ£¬Èç¹uneedle²»ÊÇ×Ö*u´®µÄ»°£¬»aµ÷ÓÃphp_needle_charº¯Êý½«needleת³ÉÕuÐÍÊý×Ö²¢×ª»»ÎªÆaASCIIÖµ¡£
²eÕÒº¯Êý
º¯Êý×iºoµ»ØµÄÊÇfound£¬php_memnstrº¯ÊýʵÏÖÁ˲eÕҵĽ*¨¡£ÄÇôÔÙ¼ÌÐø¿´¿´php_memnstrº¯Êý×oÁËʲô£º
php_memnstrÊǺ¯Êýzend_memnstrµÄºe¶¨Òa£¬²e¿´zend_memnstrº¯ÊýÈçÏ£º
static inline char *
zend_memnstr(char *haystack, char *needle, int needle_len, char *end)
{
char *p = haystack;
char ne = needle[needle_len-1];
if (needle_len == 1) {
return (char *)memchr(p, *needle, (end-p));
}
if (needle_len > end-haystack) {
return NULL;
}
// µÚÒ»¸oÓÅ»¯£¬Ö»²eÕÒend - needle_len´Î
end -= needle_len;
while (p <= end) {
// µÚ¶þ¸oÓÅ»¯£¬ÏÈÅжÏ×Ö*u´®µÄ¿ªÍ*ºÍ½aβÊÇ*ñÒ»ÑuÔÙÅжÏÕu¸o×Ö*u´®
if ((p = (char *)memchr(p, *needle, (end-p+1))) && ne == p[needle_len-1]) {
if (!memcmp(needle, p, needle_len-1)) {
return p;
}
}
if (p == NULL) {
return NULL;
}
p++;
}
return NULL;
}
µÚÒ»¸oÓÅ»¯£¬ÒoΪ(char )memchr(p, needle, (end-p+1)ÊÇÔÚend ¨C needle_len + 1£¨¼´haystack_len+1£(C)ÖвeÕÒ£¬Èç¹upΪ¿Õ£¬ËµÃ÷needleµÄµÚÒ»¸o×Ö*uÔÚpÖдÓδ³oÏÖ¹ý¡£
strstr
string strstr ( string $haystack, mixed $needle [, bool $before_needle = false ] )
µ»ØneedleÔÚhaystackÖеÚÒ»´Î³oÏÖµÄλÖõ½½aÊøµÄ×Öu´®¡£
Õa¸oº¯ÊýµÄÇø*Ö´oСдµÄ¡£
Èç¹uneedleÔÚhaystackÖв»´aeÔÚ£¬*µ»ØFALSE¡£
Èç¹ubefore_needleΪtrue£¬Ôoµ»ØhaystackÖÐneedleÔÚhaystackµÚÒ»´Î³oÏÖµÄλÖÃ֮ǰµÄ×Öu´®¡£
strstrºËÐÄÔ´Âe
if (found) {
// ¼ÆËa³ofoundµÄλÖÃ
found_offset = found - haystack;
if (part) {
RETURN_STRINGL(haystack, found_offset, 1);
} else {
RETURN_STRINGL(found, haystack_len - found_offset, 1);
}
}
strstrº¯ÊýµÄÇ°°e²¿Ö¸ustrposÀaËÆ£¬Çø±ðÔÚÓÚstrstrº¯ÊýÔÚÕÒµ½Î»Öúo£¬ÐeÒªµ»Øhaystack²¿ÖµÄ×Öu´®¡£part±aÁ¿¾ÍÊǵ÷ÓÃstrstrº¯Êýʱ´«µÝµÄbefore_needle±aÁ¿¡£
stripos
mixed stripos ( string $haystack, string $needle [, int $offset = 0 ] )
²»ÇøÖ´oСдµÄstrpos¡£ÊµÏֽʽ¸uÏÂÃaeµÄÀaËÆ£¬Ö÷ÒªÊÇʹÓÃһݿ½±´È»ºo½«ÐeÒª±È½ÏµÄ×Öu´®×ª»»³ÉСд×Ö*uºo½øÐÐÔÙ½øÐвeÕÒ¡£
stristr
string stristr ( string $haystack, mixed $needle [, bool $before_needle = false ] ) ²»Çø*Ö´oСдµÄstrstr¡£
ºËÐÄÔ´Âe
// ¿½±´Ò»*Ýhaystack
haystack_dup = estrndup(haystack, haystack_len);
if (Z_TYPE_P(needle) == IS_STRING) {
char *orig_needle;
if (!Z_STRLEN_P(needle)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty needle");
efree(haystack_dup);
RETURN_FALSE;
}
orig_needle = estrndup(Z_STRVAL_P(needle), Z_STRLEN_P(needle));
// µ÷ÓÃphp_stristrº¯ÊýÕÒ³oorig_needleµÄÖµ¡£
found = php_stristr(haystack_dup, orig_needle, haystack_len, Z_STRLEN_P(needle));
efree(orig_needle);
} else {
if (php_needle_char(needle, needle_char TSRMLS_CC) != SUCCESS) {
efree(haystack_dup);
RETURN_FALSE;
}
needle_char[1] = 0;
found = php_stristr(haystack_dup, needle_char, haystack_len, 1);
}
if (found) {
found_offset = found - haystack_dup;
if (part) {
RETVAL_STRINGL(haystack, found_offset, 1);
} else {
RETVAL_STRINGL(haystack + found_offset, haystack_len - found_offset, 1);
}
} else {
RETVAL_FALSE;
}
// ÊÍ*űaÁ¿
efree(haystack_dup);
¿ÉÒÔÖªµÀ£¬foundÊÇ´Óphp_stristrÖеõ½µÄ£¬¼ÌÐø²e¿´php_stristrº¯Êý£º
PHPAPI char *php_stristr(char *s, char *t, size_t s_len, size_t t_len)
{
php_strtolower(s, s_len);
php_strtolower(t, t_len);
return php_memnstr(s, t, t_len, s + s_len);
}
Õa¸oº¯ÊýµÄ¹¦ÄܾÍÊǽ«×Ö*u´®¶¼×ª³ÉСд֮ºoµ÷ÓÃphp_mennstrº¯ÊýÀ´²eÕÒneedleÔÚhaystackµÚÒ»´Î³oÏÖµÄλÖá£
×ܽa
ÒoΪstrpos/stripos*µ»ØµÄÊÇλÖã¬Î»ÖôÓ0¿ªÊ¼¼ÆËa£¬ËuÒÔÅжϲeÕÒʧ°Ü¶¼ÓÃ=== FALSE¸uÊʺϡ£
ÔĶÁPHPµÄÔ´ÂeÊÕ»ñͦ¶a£¬Ò»½Ãae¿ÉÒÔÖªµÀij¸oº¯ÊýµÄ¾ßÌaʵÏÖÔÀiÊÇÔoÑuµÄ£¬ÁiÒ»½Ãae¿ÉÒÔѧϰµ½Ò»Ð(C)±a³ÌÓÅ»¯*½°¸¡£
ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íu¶Ô´o¼Òѧϰphp³ÌÐoÉe¼ÆÓÐËu°iÖu¡£