很多时候,当我在 PHP 中处理字符串时,检查字符串是否包含另一个子字符串变得很重要。 PHP 有很多函数可以帮助您以您喜欢的方式操作字符串。今天我们将使用其中一些函数来学习如何检查字符串是否包含特定子字符串。
PHP 8 中添加了三个新函数来帮助我们判断一个字符串是否包含另一个子字符串。请记住,所有这些函数都区分大小写,并且检查它们是否存在空子字符串将始终返回 true
。
str_contains()
检查字符串是否包含特定子字符串。
str_ends_with()
检查字符串是否以特定子字符串结尾。
str_starts_with()
检查字符串是否以特定子字符串开头。
<?php $sentence = "Dolphins and Elephants are intelligent animals."; if(str_contains($sentence, "Elephants")) { echo 'Elephants are intelligent.'; } // Elephants are intelligent. if(str_starts_with($sentence, "Dolphins")) { echo 'Dolphins are intelligent.'; } // Dolphins are intelligent. if(str_ends_with($sentence, ".")) { echo 'There is a full stop at the end of the sentence.'; } // There is a full stop at the end of the sentence. ?>
正如我之前提到的,这些函数执行区分大小写的匹配。您还可以通过首先使用 strtolower()
或 strtoupper()
将字符串和子字符串转换为相同的大小写来与它们执行不区分大小写的匹配。
如果您不想获取有关子字符串的任何额外信息(例如它们的位置),那么这些函数是理想的选择。需要有关比赛的额外信息的人应考虑使用以下功能。
strpos()
和 stripos()
您可以使用 PHP strpos()
函数来获取字符串中子字符串第一次出现的位置。如果找不到子字符串,该函数将返回 false
。
当您使用 strpos()
检查子字符串时,请确保使用严格不等运算符。这是因为 strpos()
返回的位置是从 0 开始的,0 也可以等同于 false
。如果子字符串位于主字符串的开头,则使用常规相等性检查会给您带来误报。
以下是 strpos()
的一些示例:
<?php $sentence = "Amy, Angela, Adam and Andrew are going to a party."; if(strpos($sentence, "Angela") != false) { echo 'Angela is going to a party!'; } // Angela is going to a party! if(strpos($sentence, "Amy") != false) { echo 'Amy is going to a party! (1)'; } else { echo 'Amy is not going to a party! (1)'; } // Amy is not going to a party! (1) if(strpos($sentence, "Amy") !== false) { echo 'Amy is going to a party! (2)'; } else { echo 'Amy is not going to a party! (2)'; } // Amy is going to a party! (2) ?>
正如您在上面的示例中看到的,Angela 不在句子的开头,因此它将有一个非零位置,其计算结果为 true
。
另一方面,Amy 位于开头,其位置 0 的计算结果为 false,即使它存在于字符串中。因此,请始终确保使用 !==
来评估 strpos()
的值,因为在现实生活中处理字符串时,无法知道子字符串可能出现在哪里。
有时,您可能需要对字符串中的子字符串进行不区分大小写的检查。在这种情况下,您可以简单地使用 PHP 中的 stripos()
函数。它的工作方式与 strpos()
完全相同,但使搜索不区分大小写。
<?php $sentence = "Detect if this sentence mentions MaNGoEs."; if(strpos($sentence, "mangoes") !== false) { echo 'There was no case-sensitive mango match!'; } if(stripos($sentence, "mangoes") !== false) { echo 'There is a case-insensitive mango match!'; } // There is a case-insensitive mango match! ?>
strstr()
和 stristr()
默认情况下,strstr()
函数返回主字符串的一部分,从子字符串开始一直到主字符串的末尾。如果主字符串中不存在子字符串,它将返回 false
。
我们可以使用此信息来检查字符串中是否存在子字符串。我们所要做的就是评估 strstr()
返回的值并检查它是否为 false
。就像上次一样,我们将使用严格不等运算符 !==
来进行检查。
以下是一些示例:
<?php $sentence_a = "Is 984523850 your number?"; $sentence_b = "No, my number is 984523850"; if(strstr($sentence_a, "0") != false) { echo 'The first sentence has a 0 in it.'; } // The first sentence has a 0 in it. if(strstr($sentence_b, "0") != false) { echo 'The second sentence has a 0 in it. (1)'; } else { echo 'There is no zero in the second sentence. (1)'; } // There is no zero in the second sentence. (1) if(strstr($sentence_b, "0") !== false) { echo 'The second sentence has a 0 in it. (2)'; } else { echo 'There is no zero in the second sentence. (2)'; } // The second sentence has a 0 in it. (2) ?>
您可以使用 stristr()
代替 strstr()
以使搜索不区分大小写。 stristr()
的所有其他行为保持不变。
在这个快速提示中,我们介绍了在 PHP 中检查字符串是否包含子字符串的三种不同方法。您可以使用这些函数中的任何一个,具体取决于您需要从字符串中获取哪些信息。使用新的 PHP 8 函数将允许您跳过任何类型的严格检查,但如果您想知道子字符串的位置或获取主字符串的一部分作为返回值,则需要使用旧函数。