php 如何使用 strpos 确定输入字符串中是否存在字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6987479/
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
How to use strpos to determine if a string exists in input string?
提问by Scott B
$filename = 'my_upgrade(1).zip';
$match = 'my_upgrade';
if(!strpos($filename, $match))
{
die();
}
else
{
//proceed
}
In the code above, I'm trying to die out of the script when the filename does not contain the text string "my_upgrade". However, in the example given, it should not die since "my_upgrade(1).zip" contains the string "my_upgrade".
在上面的代码中,当文件名不包含文本字符串“my_upgrade”时,我试图退出脚本。但是,在给出的示例中,它不应该死,因为“ my_upgrade(1).zip”包含字符串“ my_upgrade”。
What am I missing?
我错过了什么?
回答by phihag
strpos
returns false
if the string is not found, and 0
if it is found at the beginning. Use the identity operatorto distinguish the two:
strpos
false
如果未找到字符串,并且0
在开头找到它,则返回。使用身份运算符区分两者:
if (strpos($filename, $match) === false) {
By the way, this fact is documented with a red background and an exclamation mark in the official documentation.
顺便说一下,官方文档中以红色背景和感叹号记录了这一事实。
回答by Rishabh
The strpos()
function is case-sensitive.
该strpos()
函数区分大小写。
if(strpos($filename, $match) !== false)
{
// $match is present in $filename
}
else
{
// $match is not present in $filename
}
For using case-insensitive.use stripos()
that is it finds the position of the first occurrence of a string inside another string (case-insensitive)
用于不区分大小写。用途stripos()
是它在另一个字符串中找到一个字符串第一次出现的位置(不区分大小写)
回答by Maxime Fafard
if (strpos($filename, $match) === false)
Otherwise, strpos
will return 0
(the index of the match), which is false
.
否则,strpos
将返回0
(匹配的索引),即false
。
The ===
operator will also compare type of the variables (boolean != integer)
该===
运营商也将比较变量的类型(布尔!=整数)
回答by hakre
false === strpos($filename, $match)
The strpos
functionDocsreturns false
if not found or 0
if found on position 0
(programmers like to start counting at 0
often):
该strpos
函数的文档返回false
,如果没有找到或者0
如果在位置上被发现0
(程序员喜欢在开始计数0
常):
WarningThis function may return Boolean
FALSE
, but may also return a non-Boolean value which evaluates toFALSE
, such as0
or""
. Please read the section on Booleans for more information. Use the===
operator for testing the return value of this function.
警告此函数可能返回 Boolean
FALSE
,但也可能返回计算结果为 的非布尔值FALSE
,例如0
或""
。请阅读有关布尔值的部分以获取更多信息。使用===
运算符来测试这个函数的返回值。
回答by Atanas Atanasov
This working for me when everything other fail in some situations:
当其他一切在某些情况下失败时,这对我有用:
$filename = 'my_upgrade(1).zip';
$match = 'my_upgrade';
$checker == false;
if(strpos($filename, $match))
{
$checker == true;
}
if ($checker === false)
{
die();
}
else
{
//proceed
}
Or in short:
或者简而言之:
$filename = 'my_upgrade(1).zip';
$match = 'my_upgrade';
$checker == false;
if(strpos($filename, $match))
{
$checker == true;
//proceed
}
if ($checker === false)
{
die();
}
回答by Abhishek Gupta
$data = 'match';
$line = 'match word in this line';
if(strpos($line,$data) !== false){
echo "data found";
}else{
echo "no data found";
}
回答by John Percival Hackworth
strpos in this case will return a zero, which is then interpretted as false when you do the logical negation. You should check explicitly for the boolean false
.
在这种情况下 strpos 将返回一个零,然后在您进行逻辑否定时将其解释为 false。您应该明确检查 boolean false
。