简体   繁体   English

平等不适用于PHP

[英]equality doesnt work with php

I am trying to check two sentences in greek equal with this function: 我试图用这个函数检查希腊语中的两个句子:

   private function process_line_three($target_line_three,$case_line_three){
      $target_line_three=trim($target_line_three);
       $case_line_three=trim($case_line_three);
      echo "<br/><br/>";
       var_dump($case_line_three);
        echo "<br/><br/>";
        echo mb_detect_encoding($target_line_three);
        echo "<br/><br/>";
         echo mb_detect_encoding($case_line_three);
       //$this->print_chars_not_equal($target_line_three,$case_line_three);

   return strcasecmp($target_line_three, $case_line_three)==0;
   }

When I pass them the following two strings I get this: 当我传递以下两个字符串时,我得到了这个:

Line 3: case Συνδέσου με χιλιάδες ανθρώπους Παρακολούθησε & δράσε ζωντανά 24/7==Συνδέσου με χιλιάδες ανθρώπους Παρακολούθησε & δράσε ζωντανά 24/7

But equality fails here.. i return false on that result, even though both sentences are similar.. why is that so? 但是这里的平等失败了......我对这个结果都是假的,即使两个句子都相似,为什么会这样呢?

UPDATE : 更新:

var_dump($case_line_three); 后续代码var_dump($ case_line_three); string(117) "Συνδέσου με χιλιάδες ανθρώπους Παρακολούθησε & δράσε ζωντανά 24/7" string(117)“ΣυνδέσουμεχιλιάδεςανθρώπουςΠαρακολούθησε&δράσεζωντανά24/ 7”

meanwhile, i am trying to find an encoding issue 同时,我试图找到一个编码问题

UPDATE 2: the output is: 更新2:输出是:

string(117) "Συνδέσου με χιλιάδες ανθρώπους Παρακολούθησε & δράσε ζωντανά 24/7" 

UTF-8 UTF-8

UTF-8 UTF-8

both characters are utf 两个字符都是utf

UPDATE 3 output when I write to a file: 写入文件时更新3输出:

Συνδέσου με χιλιάδες ανθρώπους Παρακολούθησε &amp; δράσε ζωντανά 24/7

Συνδέσου με χιλιάδες ανθρώπους Παρακολούθησε & δράσε ζωντανά 24/7

You could try: 你可以尝试:

if(strcmp ($target1 , $target2 ) == 0);

A second alternative will be using a multibyte encoding insensitive comparison.... 第二种选择是使用多字节编码不敏感的比较....

function mb_strcasecmp($target1, $target2, $encoding = null) {
    if (null === $encoding) { $encoding = mb_internal_encoding(); }
    return strcmp(mb_strtoupper($target1, $encoding), mb_strtoupper($target2, $encoding)); }

尝试使用PHP本机方法strcasecmp() - http://php.net/strcasecmp

You could use strncmp to compare the two strings character for character, stopping when they are not equal. 您可以使用strncmp比较字符的两个字符串字符,当它们不相等时停止。 Then you could determine which character(s) is different. 然后你可以确定哪个角色是不同的。 levenshtein might tell you how many characters are different. levenshtein可能会告诉你有多少个字符是不同的。

$a = 'Συνδέσου με χιλιάδες ανθρώπους Παρακολούθησε & δράσε ζωντανά 24/7';
$b = 'Συνδέσου με χιλιάδες ανθρώπους Παρακολούθησε & δράσε ζωντανά 24/7';

$i=1; $l=strlen($a); $equals=true;
while ($i<=$l && $equals) {
    if (strncmp($a, $b, $i) != 0) {
        $equals = false;
    } else {
        $i++;
    }
}
if ($equals) {
    echo 'Strings are equal';
} else {
    echo 'Character '.$i.' is not equal';
}

Strings are equal for me, so it is probably an encoding issue. 字符串对我来说是相同的,所以它可能是一个编码问题。 You could use mb-detect-encoding to check the encoding of the offending characters http://www.php.net/manual/en/function.mb-detect-encoding.php 您可以使用mb-detect-encoding来检查有问题的字符的编码http://www.php.net/manual/en/function.mb-detect-encoding.php

Could there be an encoding problem? 可能有编码问题吗?

Try using this: 试试这个:

return (strcmp($target_line_three, $case_line_three) == 0);

strcmp() is case-sensitive. strcmp()区分大小写。 For case insensitive, use strcasecmp() 对于不区分大小写,请使用strcasecmp()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM