Issue
I’m working on a feature which requires me to get the contents of a webpage, and then check to see if certain text is present in that page. It’s a backlink checking tool.
The problem is this – the function runs perfectly most of the time, but occasionally, it flags a page for not having a link when the link is clearly there. I’ve tracked it down to the point of visually comparing the strings in the output, and they match just fine, but using the == operator, PHP tells me they don’t match.
Recognizing that this is probably some sort of encoding issue, I decided to see what would happen if I used base64_encode() on them, so I could see if doing so produced different results between the two strings (which appear to be exactly the same).
My suspicions were confirmed – using base64_encode on the strings to be compared yielded a different string from each. The problem was found!
Is there some way I can make these strings uniform based on the outputted text (which matches), so that when I compare them in PHP, they match?
Solution
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;
}
}
}
Answered By – Kip
Answer Checked By – Marilyn (BugsFixing Volunteer)