gpt4 book ai didi

php 版本的 pythons if in

转载 作者:行者123 更新时间:2023-12-03 04:07:25 25 4
gpt4 key购买 nike

Python 有一个很棒的功能:如果字符串中有子字符串,如下所示:

if "fox" in "The quick brown fox jumps over the lazy dog":
print True

PHP 有类似的东西吗?

最佳答案

是的,请参阅striposstrpos

if(stripos("The quick brown fox jumps over the lazy dog","fox")!==false)
{ echo 'True'; }

stripos 和 strpos 返回匹配开始的位置,因此需要使用严格比较(=== 或 !==)以避免针在 pos 时出现漏报。 0 大海捞针。

您可能更喜欢stristr ,它只返回匹配的字符串或 bool 值 false。

in_array如果您还考虑字符串之外的内容,则其工作方式与 Python 中的 in 的列表和字典大致相同。

关于php 版本的 pythons if in,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4339972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com