Encoded data and spaces

Then the spaces are not truly spaces, they are likely considered “low” values (meaning before the space on the ASCII table/your encoding table).

You may want to try this (although it may still not work, because the value may not match)

$str = preg_replace('/[\\s]/', '', $str); //replaces whitespace, form feeds, tabs, carriage returns and new lines

Another alternative, is if you are expecting only letters and numbers in your conversion, you can remove anything that is not a letter or number

$str = preg_replace('/[^a-z0-9]+/i', '', $str);