PHP字符串“包含" [英] PHP string "contains"

查看:89
本文介绍了PHP字符串“包含"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What would be the most efficient way to check whether a string contains a "." or not?

我知道您可以通过正则表达式之类的许多不同方式进行操作或循环遍历该字符串以查看其是否包含一个点(.").

I know you can do this in many different ways like with regular expressions or loop through the string to see if it contains a dot (".").

推荐答案

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

请注意,您需要与!==运算符进行比较.如果使用!=<>并且'.'在位置0上找到,嘿! 0比较等于FALSE,您输了.这样一来,您就可以在周末将生产网站指向开发数据库,​​而在星期一返回时就不会感到无穷无尽.

Note that you need to compare with the !== operator. If you use != or <> and the '.' is found at position 0, hey! 0 compares equal to FALSE and you lose. This will cause you to point a production website at a development database over the weekend, causing no end of joy when you return monday.

这篇关于PHP字符串“包含"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆