[英]PHP String Comparison Not Working
I am having an issue with PHP String comparison. 我在使用PHP字符串比较时遇到问题。 It never turns out to be 0. Below I am trying to get a setting in a cookie and compare it with a given key, and return the value of that cookie.
它永远不会变成0。下面,我试图在cookie中获取一个设置,并将其与给定键进行比较,然后返回该cookie的值。 The cookies looks like this console=1|userId=5.
cookie看起来像这个console = 1 | userId = 5。 The value is always -180 or something.
该值始终为-180或其他值。 I echo them out and the are the same, and the lengths are the same.
我回显它们,并且它们相同,并且长度相同。 Could it be an encoding issue?
可能是编码问题吗?
$parameters = explode('|', $_COOKIE[Cisco_Rewards::REWARDS_SETTINGS_COOKIE_NAME]);
for ($i = 0; $i < count($parameters); $i++) {
$parameter = explode('=', $parameters[$i]);
if(count($parameter) > 1) {
echo strcasecmp(strtolower(trim($parameter[0])), trim(strtolower($name)));
if(strcasecmp(strtolower(trim($parameter[0])), trim(strtolower($name))) == 0) {
return $parameter[1];
}
}
}
You're missing break; 你错过了休息; in for loop !
在for循环中 !
Yes, it could be encoding issue because function strcasecmp() works with Unicode only. 是的,这可能是编码问题,因为函数strcasecmp()仅适用于Unicode。 Multi-byte characters such as UTF-8 or UTF-16 cannot be compared with strcasecmp() .
多字节字符(例如UTF-8或UTF-16)不能与strcasecmp()进行比较。
Also, strcasecmp() is case-insensitive function so using strtolower() against its parameters doesn't change function's result (string "example" is same as "EXAMPLE"m "eXaMPlE", "ExamPlE", etc). 另外, strcasecmp()是不区分大小写的函数,因此对其参数使用strtolower()不会更改函数的结果(字符串“ example”与“ EXAMPLE”,“ eXaMPlE”,“ ExamPlE”等相同)。
You should set default result value (like $res=false;
) to be sure that result is set after loop. 您应该设置默认结果值(例如
$res=false;
),以确保在循环后设置结果。
You should replace for loop block with foreach loop like this one bellow 您应该像下面这样用foreach循环替换for循环块
$parameters = explode('|', $_COOKIE[Cisco_Rewards::REWARDS_SETTINGS_COOKIE_NAME]);
// this will handle result
$res = false;
foreach ($parameters as $value) {
$param = explode('=', $value);
if(count($parameter) > 1) {
// I guess this line is just for testing result
echo "param1=".trim($param[0])."; param2=".trim($name)."; Result=".strcasecmp(trim($param[0]), trim($name)) . "<br />\n";
if(strcasecmp(trim($param[0]), trim($name))) {
$res=$param[1];
// you should break here
break;
}
}
}
// try to output result before (testing purpose only)
var_dump($res);
return $res;
But to make this solution easy you can use this function 但是要使此解决方案简单易用,您可以使用此功能
function getParamValue($parameters, $key) {
$res = false;
$plist = explode('|', $parameters);
foreach ($plist as $pair) {
$element = explode('=', $pair);
if (trim($element[0]) == trim($key)) {
$res = trim($element[1]);
break;
}
}
return $res;
}
So if you have $parameters
string like "console=1|userid=8159"
and want to find value for $key
string "userid"
, put it into this function and it will return your result or false if $key
was not found in $parameters
list. 因此,如果您有
$parameters
字符串,例如"console=1|userid=8159"
并想为$key
字符串"userid"
查找值,则将其放入此函数中,它将返回结果;如果未在$key
中找到$key
,则返回false $parameters
列表。
Example 例
$parameters = "console=1|userid=8159";
$name = "userid";
echo getParamValue($parameters, $name);
Output 输出量
8159
Now you can write your code this way 现在您可以用这种方式编写代码
$parameters = explode('|', $_COOKIE[Cisco_Rewards::REWARDS_SETTINGS_COOKIE_NAME]);
$value = getParamValue($parameters, $name);
And $value takes your result or returns false if $name is not in $parameters list. $ value接受结果,或者如果$ name不在$ parameters列表中,则返回false 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.