Search code examples
phpstringtypescomparison

PHP string comparison for strings which look the same in some views, but not in others


I have two strings that look the same when I echo them, but when I var_dump() them they are different string types:

Echo:

http://blah
http://blah

var dump:

string(14) "http://blah"
string(11) "http://blah"

strToHex:

%68%74%74%70%3a%2f%2f%62%6c%61%68%00%00%00
%68%74%74%70%3a%2f%2f%62%6c%61%68

When I compare them, they return false. How can I manipulate the string type, so that I can perform a comparison that returns true?

What is the difference between string 11 and string 14? I am sure there is a simple resolution, but I have not found anything yet. No matter how I implode, explode, UTF-8 encode, etc., they will not compare the strings or change type.


Solution

  • Trim the strings before comparing. There are escaped characters, like \t and \n, which are not visible.

    $clean_str = trim($str);