PHP 字符串 "contains"

标签 php regex string

检查字符串是否包含“.”的最有效方法是什么?还是不行?

我知道你可以通过许多不同的方式来做到这一点,比如 regular expressions或循环遍历字符串以查看它是否包含点(“.”)。

最佳答案

PHP 8 或更新版本:

使用 str_contains 功能。

if (str_contains($str, "."))
{
    echo 'Found it';
}

else
{
    echo 'Not found.';
}

PHP 7 或更早版本:

if (strpos($str, '.') !== FALSE)
{
    echo 'Found it';
}

else
{
    echo 'Not found.';
}

请注意,您需要使用 !==运算符(operator)。如果您使用 !=<>'.'位于位置 0 ,比较结果为真,因为 0大致等于 false .

关于PHP 字符串 "contains",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13577041/

相关文章:

php - tests.php 问题 - sipp webfrontend

php - Mysql将4个不同表的信息显示为一个表

python - 如何删除Python中与正则表达式匹配的所有字符串?

regex - (.)+ 在正则表达式中的行为是什么?

ios - Swift 过滤字符串数组

php - 如何在 Swift 中访问从 PHP 传递的数组中的数据?

PHP/MySQL : Joining three tables and merging results

javascript - 正则表达式:在 url 登录/测试中获取字符串

php - 从字符串 php 中提取顶级域名

c - 使用最少代码行的系统设置将原始字节写入串行 (linux C)