Make encoding uniform before comparing strings in PHP

后端 未结 6 1549
攒了一身酷
攒了一身酷 2021-01-20 16:21

I\'m working on a feature which requires me to get the contents of a webpage, then check to see if certain text is present in that page. It\'s a backlink checking tool.

6条回答
  •  孤城傲影
    2021-01-20 16:26

    I'm not entirely sold on your belief that it is the encoding. PHP is going to internally store all its strings in the same format. Could you try this code? It will compare the ascii value of each character in both strings, which might reveal something you're not seeing by visually comparing the strings.

    $str1 = ...;
    $str2 = ...;
    
    if(strlen($str1) != strlen($str2)) {
      echo "Lengths are different!";
    } else {
      for($i=0; $i < strlen($str1); $i++) {
        if(ord($str1[$i]) != ord($str2[$i]) {
          echo "Character $i is different! str1: " . ord($str1[$i]) . ", str2: " . ord($str2[$i]);
          break;
        }
      }
    }
    

提交回复
热议问题