在PHP中克服针对栈的困惑

19

在PHP中,最实用的克服“针头乱码”问题的方法是什么?

这里$needle是第一个参数。

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

这里的 $needle 是第二个参数。

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

8
https://gist.github.com/1672543 - Gordon
@Gordon,我把这个函数列表与get_defined_functions()的内部数组交换了一下,发现这个列表中缺少了mb字符串函数和iconv函数。但它还是不错的;-? - TerryE
1
根据Gordon的链接:字符串函数是haystack、needle和数组函数是needle、haystack。 - teynon
我尝试了一个Str类,将顺序统一为“needle, haystack”:https://github.com/dereuromark/tools/blob/master/Lib/Utility/Str.php。虽然我没有检查这里的性能开销或速度损失。 - mark
没有混淆。字符串函数以干草堆开始,而数组函数以针开始。 - Wael Assaf
显示剩余2条评论
3个回答

12

如果您将其视为中缀运算的前缀表示,则这可能是有意义的。

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)

1
但字符串是字符数组。 <?php $str =“foo”; echo $str [2]; ?> - user1440875
3
为什么句子 does $haystack strstr $needleis $needle strstr $haystack 更合乎逻辑? - Nanne
@Nanne 更多关于助记符而非逻辑。语句“x是否在y中”含有“in”,因此in_array函数的第一个参数将是x,第二个参数将是y。在带有“does”的句子中,y在x之前。因此该函数将首先取y,然后再取x。 - Kinjal Dixit
@Nanne 你必须考虑函数的含义,我们在编码时都有自己的方式,构建这个PHP函数的人是一个人类,strstr听起来像是他们在思考元字符串函数,所以str(str(...这有点说得通... strpos=字符串是否有位置...如果它是posstr(),是不合理的,但是小关键字的顺序有帮助,作为一个以英语为母语的人,无法避免地会这样阅读... strrchr不能轻易地被读作是字符串中的字符....而是表示字符串是否有字符....请注意这些函数名称的顺序。 - Neo

6
我为所有 PHP 命令制作了一张备忘单,其中包含针和沙堆参数。
字符串函数中,haystack/needle 的顺序是固定的。
          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

数组函数主要是针对于 needle/haystack 的操作(除了 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 $array [, callable $callback [, int $flag = 0 ]] ) : array

这个函数可以用来过滤数组中不需要的值,并返回新的数组。参数$array是输入数组,$callback是可选参数,用于设定过滤函数,$flag也是可选参数,用于设定过滤标识。该函数会返回一个新的数组。

-1

我认为这并不重要。这与Linux bash相同。例如,tar使用参数存档文件,而ln使用参数目标链接名称。但我的例子不是一种编程语言,所以这里有另一个解释:世界上第一个计数或编号是什么?对于你需要的东西,总是需要两个参数。对于我的tar和ln示例也是如此。


我认为像这样的不一致性问题是让我离开PHP去学习其他编程语言的原因,所以对于初学者来说,记忆技巧和解决问题的帮助非常重要。 - Zev
@Zev:在我看来,编号和计数并不是PHP特有的。 - Micromega

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接