判断PHP字符串是否包含另一字符串,核心是使用strpos()或strstr()函数;前者返回位置,后者返回剩余部分,需用!==false判断是否存在;区分大小写时用strpos()/strstr(),不区分用stripos()/stristr();PHP 8.0+可用str_starts_with()/str_ends_with()判断开头结尾;多字节字符应使用mb_strpos()/mb_strstr()等多字节安全函数;正则匹配用preg_match()但需防注入;性能上strpos()略快于strstr(),实际差异通常可忽略。

判断PHP字符串是否包含另一个字符串,核心在于使用
strpos()
strstr()
解决方案
PHP提供了多种方法来判断一个字符串是否包含另一个字符串,最常用的是
strpos()
strstr()
strpos()
false
立即学习“PHP免费学习笔记(深入)”;
$haystack = "This is a sample string.";
$needle = "sample";
$position = strpos($haystack, $needle);
if ($position !== false) {
echo "字符串 '$needle' 包含在字符串 '$haystack' 中,位置是: " . $position;
} else {
echo "字符串 '$needle' 不包含在字符串 '$haystack' 中。";
}
// 输出:字符串 'sample' 包含在字符串 'This is a sample string.' 中,位置是: 10注意:
strpos()
false
!==
false
strstr()
false
$haystack = "This is a sample string.";
$needle = "sample";
$result = strstr($haystack, $needle);
if ($result !== false) {
echo "字符串 '$needle' 包含在字符串 '$haystack' 中,剩余部分是: " . $result;
} else {
echo "字符串 '$needle' 不包含在字符串 '$haystack' 中。";
}
// 输出:字符串 'sample' 包含在字符串 'This is a sample string.' 中,剩余部分是: sample string.stripos()
strpos()
$haystack = "This is a Sample string.";
$needle = "sample";
$position = stripos($haystack, $needle);
if ($position !== false) {
echo "字符串 '$needle' 包含在字符串 '$haystack' 中,位置是: " . $position;
} else {
echo "字符串 '$needle' 不包含在字符串 '$haystack' 中。";
}
// 输出:字符串 'sample' 包含在字符串 'This is a Sample string.' 中,位置是: 10stristr()
strstr()
$haystack = "This is a Sample string.";
$needle = "sample";
$result = stristr($haystack, $needle);
if ($result !== false) {
echo "字符串 '$needle' 包含在字符串 '$haystack' 中,剩余部分是: " . $result;
} else {
echo "字符串 '$needle' 不包含在字符串 '$haystack' 中。";
}
// 输出:字符串 'sample' 包含在字符串 'This is a Sample string.' 中,剩余部分是: Sample string.PHP判断字符串包含关系时,性能上有什么差异?
一般来说,
strpos()
strstr()
strpos()
strstr()
stripos()
stristr()
如何判断一个字符串是否以另一个字符串开头或结尾?
PHP提供了
str_starts_with()
str_ends_with()
substr()
str_starts_with()
$haystack = "This is a sample string.";
$needle = "This";
if (str_starts_with($haystack, $needle)) {
echo "字符串 '$haystack' 以 '$needle' 开头。";
} else {
echo "字符串 '$haystack' 不以 '$needle' 开头。";
}
// 输出:字符串 'This is a sample string.' 以 'This' 开头。str_ends_with()
$haystack = "This is a sample string.";
$needle = "string.";
if (str_ends_with($haystack, $needle)) {
echo "字符串 '$haystack' 以 '$needle' 结尾。";
} else {
echo "字符串 '$haystack' 不以 '$needle' 结尾。";
}
// 输出:字符串 'This is a sample string.' 以 'string.' 结尾。使用 substr()
str_starts_with()
$haystack = "This is a sample string.";
$needle = "This";
if (substr($haystack, 0, strlen($needle)) === $needle) {
echo "字符串 '$haystack' 以 '$needle' 开头。";
} else {
echo "字符串 '$haystack' 不以 '$needle' 开头。";
}
// 输出:字符串 'This is a sample string.' 以 'This' 开头。使用 substr()
str_ends_with()
$haystack = "This is a sample string.";
$needle = "string.";
if (substr($haystack, -strlen($needle)) === $needle) {
echo "字符串 '$haystack' 以 '$needle' 结尾。";
} else {
echo "字符串 '$haystack' 不以 '$needle' 结尾。";
}
// 输出:字符串 'This is a sample string.' 以 'string.' 结尾。如何使用正则表达式判断字符串包含关系?
正则表达式提供了一种更灵活的字符串匹配方式。 你可以使用
preg_match()
$haystack = "This is a sample string.";
$pattern = "/sample/"; // 匹配 "sample"
if (preg_match($pattern, $haystack)) {
echo "字符串 '$haystack' 包含模式 '$pattern'。";
} else {
echo "字符串 '$haystack' 不包含模式 '$pattern'。";
}
// 输出:字符串 'This is a sample string.' 包含模式 '/sample/'。正则表达式的优点在于可以进行更复杂的匹配,例如匹配多个可能的字符串,或者匹配符合特定模式的字符串。例如,你可以使用
/s[a-z]+e/
如何处理多字节字符的字符串包含判断?
对于包含多字节字符(例如中文、日文、韩文)的字符串,需要使用
mb_strpos()
mb_strstr()
strpos()
strstr()
mbstring
$haystack = "这是一个包含中文的字符串。";
$needle = "中文";
$position = mb_strpos($haystack, $needle);
if ($position !== false) {
echo "字符串 '$needle' 包含在字符串 '$haystack' 中,位置是: " . $position;
} else {
echo "字符串 '$needle' 不包含在字符串 '$haystack' 中。";
}
// 输出:字符串 '中文' 包含在字符串 '这是一个包含中文的字符串。' 中,位置是: 5同样,也有
mb_stripos()
mb_stristr()
如何避免在判断字符串包含关系时出现安全问题?
当处理用户输入时,需要特别注意安全问题。 如果你将用户输入直接用于正则表达式,可能会导致正则表达式注入攻击。 为了避免这种情况,应该对用户输入进行适当的验证和转义。
另外,在使用
strpos()
以上就是PHP如何判断一个字符串是否包含另一个字符串_PHP检查字符串包含关系的方法的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号