[英]why strpos doesn't work with “>” and other chracters
I want to check whether special characters such as "<" ">" or the double quote itself is found in a string. 我想检查是否在字符串中找到特殊字符(例如“ <”>”或双引号本身)。 But my function always return true.
但是我的函数总是返回true。
Thank you 谢谢
This is the code: 这是代码:
$name = "<h1><dfdafdfds";
function correctName($name){
if (strlen($name) < 5 || (strpos($name, "<")===true) ||
(strpos($name, ">")===true) || (strpos($name, "\"")===true)){
return false;
}else{
return true;
}
}
Strpos either returns false
or an integer value such as 5
. Strpos返回
false
或整数值,例如5
。 It does NOT return true
. 它不会返回
true
。
Therefore (strpos($name, "<")===true
always returns false. 因此
(strpos($name, "<")===true
始终返回false。
your code evaluates as: 您的代码评估为:
if (strlen($name) < 5 || false) ||
(false) || (false)){
return false;
}else{
return true;
}
You need to use this format: 您需要使用以下格式:
strpos($name, '<') !== false
so your code should look like: 因此您的代码应如下所示:
if (strlen($name) < 5 || strpos($name, "<") !== false || strpos($name, ">") !== false || strpos($name, "\"") !== false) {
strpos
never returns TRUE
. strpos
永远不会返回TRUE
。 It might return FALSE
. 它可能返回
FALSE
。 Solution: change your comparisons to !== FALSE
解决方案:将您的比较更改为
!== FALSE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.