克服 PHP 中的针头大海捞针混乱

2022-08-30 23:37:20

在PHP中克服针头大海捞针混乱的最实用方法是什么?

这里$needle是第一个参数

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

这里$needle是第二个参数

string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )

答案 1

如果您将其视为修复内操作的预修复表示形式,则可能有意义。

is "bat" in array ("cat", "rat", "bat", "fat")
is $needle in_array $haystack
in_array($needle, $haystack)


does "supercalifragistic" string contain string "percal"
does $haystack strstr $needle
strstr($haystack, $needle)

答案 2

我为所有包含针式干草堆参数的php命令制作了一个备忘单。

字符串函数是大海捞针/针:

          strpos $haystack, $needle
         stripos $haystack, $needle
          strstr $haystack, $needle
          strchr $haystack, $needle
         stristr $haystack, $needle
         strrchr $haystack, $needle
        strripos $haystack, $needle
         strrpos $haystack, $needle
    substr_count $haystack, $needle

strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int
stripos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int
strstr ( string $haystack , mixed $needle [, bool $before_needle = FALSE ] ) : string
(same as strstr) strchr ( string $haystack , mixed $needle [, bool $before_needle = FALSE ] ) : string
stristr ( string $haystack , mixed $needle [, bool $before_needle = FALSE ] ) : string
strrchr ( string $haystack , mixed $needle ) : string
strripos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int
strrpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int
substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] ) : int

数组函数是针/大海捞针(array_filter除外):

    array_search $needle, $haystack
        in_array $needle, $haystack

in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) : bool
array_search ( mixed $needle  , array $haystack  [, bool $strict  ] )

array_filter ( 数组 $array [, 可调用$callback [, int $flag = 0 ]] ) : 数组


推荐