php 检测是否包含符合 -
在PHP中,可以使用strpos()函数来检测一个字符串是否包含特定的子字符串。
以下是一个示例代码:
$string = "This is a sample string";
$substring = "-";
if (strpos($string, $substring) !== false) {
echo "String contains the substring";
} else {
echo "String does not contain the substring";
}
在这个例子中,我们首先定义了一个字符串 $string,然后定义了一个要检测的子字符串 $substring。然后,我们使用 strpos() 函数来检查 $string 中是否包含 $substring。如果包含,则返回其在字符串中的位置,如果不包含,则返回 false。通过使用 !== 运算符,我们可以确保返回的位置不是 false。如果返回的位置不是 false,则输出 "String contains the substring",否则输出 "String does not contain the substring"。
请注意,strpos() 函数是区分大小写的,如果你想要进行大小写不敏感的匹配,可以使用 stripos() 函数
原文地址: https://www.cveoy.top/t/topic/igfZ 著作权归作者所有。请勿转载和采集!