[英]PHP compare strings with hyphen
I need to compare 2 strings without replacing anything. 我需要比较2个字符串而不替换任何东西。
$str1 = 'short-eared owl';
$str2 = 'short-eared owl';
if (strcmp($str1, $str2)===0) { do this and that …}
The problem here is the hyphen. 这里的问题是连字符。 Even both strings are equal, strcmp() does not result in 0 (equal).
即使两个字符串都相等,strcmp()也不等于0(等于)。 Why ?
为什么呢
How to compare those strings correct ? 如何比较那些字符串正确?
UPDATE 更新
Here is the essential piece of code 这是必不可少的代码
<?php
$cat=$_GET["cat"];
if (($handle = fopen($csvFile, "r")) !== FALSE) {
$dataArray = fgetcsv($handle, 1000, ";");
while (($dataArray = fgetcsv($handle, 1000, ";")) !== FALSE) {
for ($i=0; $i < 4 ; $i++) {
if (strcmp($cat,$dataArray[$i])===0 ) {
// do this and that
}
}
}
fclose($handle);
}
?>
What I do is comparing $_GET var which is a string like 'short-eared owl' against columns of a cvs file. 我所做的是将$ _GET var与cvs文件的列进行比较,该字符串是“短耳猫头鹰”之类的字符串。 If I found 'short-eared owl' in that file I return.
如果在该文件中找到“短头猫头鹰”,我将返回。 But strcmp() does not work here because of the hyphen.
但是由于连字符,strcmp()在这里不起作用。 If I delete the hypen manually for testing purposes strcmp() matches.
如果出于测试目的手动删除了hypen,则strcmp()匹配。
UDATE 2 UDATE 2
When evaluating the strcmp() function at php site here there is an "Anonymous" User Contribute Note talking about collation
. 在php站点上评估strcmp()函数时, 这里有一个“匿名”用户贡献注释,它涉及
collation
。 He writes : 他写 :
In summary, strcmp() does not necessarily use the ASCII code order of each character like in the 'C' locale, but instead parse each string to match language-specific character entities (such as 'ch' in Spanish, or 'dz' in Czech), whose collation order is then compared.
总之,strcmp()不一定像“ C”语言环境那样使用每个字符的ASCII码顺序,而是解析每个字符串以匹配特定于语言的字符实体(例如西班牙语的“ ch”或“ dz” (捷克文),然后比较其排序顺序。 When both character entities have the same collation order (such as 'ss' and ' ' in German), they are compared relative to their code by strcmp(), or considered equal by strcasecmp().
当两个字符实体具有相同的排序规则顺序(例如德语中的“ ss”和“ ...”)时,它们将通过strcmp()相对于其代码进行比较,或者被strcasecmp()视为相等。
If this is the problem with the hyphen char how to solve it ? 如果这是连字符问题,该如何解决?
the code you wrote works fine i checked it myself like 您编写的代码很好用,我自己检查了一下
$str1 = 'short-eared owl';
$str2 = 'short-eared owl';
if (strcmp($str1, $str2)===0) {
echo "success";
} else{
echo 'fail';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.