PHP 字符串“包含”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13577041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:42:54  来源:igfitidea点击:

PHP string "contains"

phpregexstring

提问by ealeon

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 expressionsor loop through the string to see if it contains a dot (".").

我知道您可以通过许多不同的方式来执行此操作,例如使用正则表达式或遍历字符串以查看它是否包含点(“.”)。

回答by akatakritos

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

Note that you need to compare with the !==operator. If you use !=or <>and the '.'is found at position 0, hey! 0compares equal to FALSEand 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.

请注意,您需要与!==运算符进行比较。如果您使用!=<>并且'.'在位置找到0,嘿!0比较等于FALSE你输了。这将导致您在周末将生产网站指向开发数据库,​​从而在您周一返回时带来无尽的欢乐。

回答by Muthu Kumaran

You can use these string functions,

您可以使用这些字符串函数,

strstr— Find the first occurrence of a string

strstr— 查找第一次出现的字符串

stristr— Case-insensitive strstr()

stristr— 不区分大小写的 strstr()

strrchr— Find the last occurrence of a character in a string

strrchr— 查找字符串中最后一次出现的字符

strpos— Find the position of the first occurrence of a substring in a string

strpos— 查找子字符串在字符串中第一次出现的位置

strpbrk— Search a string for any of a set of characters

strpbrk— 在字符串中搜索一组字符中的任何一个

If that doesn't help then you should use pregregular expression

如果那没有帮助,那么您应该使用preg正则表达式

preg_match— Perform a regular expression match

preg_match— 执行正则表达式匹配

回答by Sylverdrag

You can use stristr()or strpos(). Both return falseif nothing is found.

您可以使用 stristr()strpos()。如果没有找到,两者都返回 false