• 日常搜索
  • 百度一下
  • Google
  • 在线工具
  • 搜转载

如何在PHP中检查一个字符串是否包含另一个子字符串

很多时候,当我在 php 中处理字符串时,检查一个字符串是否包含另一个子字符串变得很重要。PHP 有很多函数可以帮助您以任何您喜欢的方式操作字符串。我们今天将使用其中一些函数来学习如何检查字符串是否包含特定的子字符串。

如何在PHP中检查一个字符串是否包含另一个子字符串  第1张

在 PHP 8 中使用新函数

PHP 8 中添加了三个新函数来帮助我们判断一个字符串是否包含另一个子字符串。请记住,所有这些函数都区分大小写,并且使用它们检查是否存在空子字符串将始终返回true。

  1. str_contains()检查字符串是否包含特定的子字符串。

  2. str_ends_with()检查字符串是否以特定子字符串结尾。

  3. 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()您还可以通过首先使用or将字符串和子字符串都转换为相同的大小写来对它们执行不区分大小写的匹配strtoupper()。

如果您不想获取有关子字符串的任何额外信息,例如它们的位置,这些函数是理想的。需要有关比赛的额外信息的人应考虑使用以下功能。

使用strpos()和stripos()

您可以使用 PHPstrpos()函数来获取字符串中子字符串第一次出现的位置。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)

?>

正如你在上面的例子中看到的,安吉拉不在我们句子的开头,所以它会有一个非零位置,计算结果为true。

另一方面,Amy 在开头是正确的,它的位置 0 评估为 false,即使它存在于字符串中。因此,请始终确保评估 with 的值,strpos()因为!==在现实生活中处理字符串时,无法知道子字符串可能出现的位置。

有时,您可能需要对字符串中的子字符串进行不区分大小写的检查。在这种情况下,您可以简单地使用stripos()PHP 中的函数。它的工作原理完全一样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 函数将允许您跳过任何类型的严格检查,但如果您想知道子字符串的位置或获取主字符串的一部分作为返回值,则需要使用旧函数。


文章目录
  • 在 PHP 8 中使用新函数
  • 使用strpos()和stripos()
  • 使用strstr()和stristr()
  • 最后的想法