当前位置:编程学习 > php >>

php8判断字符串是否存在某个字符的函数

php8 如何检测字符串在 PHP8 中,检测字符串有以下几种常用方法:

检测字符串是否包含子字符串使用 str_contains() 函数,语法为 str_contains($haystack, $needle)。例如:
phpif (str_contains('yizuotu.net', 'yizuotu')) {
    echo "找到了";
}


检测字符串开头使用 str_starts_with() 函数,语法为 str_starts_with($haystack, $needle)。例如:
phpif (str_starts_with('https://yizuotu.com', 'https')) {
    echo "以 https 开头";
}


检测字符串结尾使用 str_ends_with() 函数,语法为 str_ends_with($haystack, $needle)。例如:
phpif (str_ends_with('zzzyk.com', '.com')) {
    echo "以 .com 结尾";
}



这些函数在 PHP8 中直接返回布尔值true/false,代码简洁且可读性高

避免了传统 strpos() 函数可能出现的 false 和 0 的混淆问题。

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,