Two identical strings, yet different lengths. Trimmed, cleansed, etc
I have two strings, one is generated by PHP and stored in a database at 128 characters. The generated string is e-mailed to the user and the开发者_运维问答y must input the string.
Now for some reason, the one from the user (with me testing), is 132 characters long through var_dump
. When I echo
them, they are exactly the same. Same thing for var_dump
. Except the character count. Where are these extra 4 (invisible) characters coming from?
try
var_dump(bin2hex($str1));
var_dump(bin2hex($str2));
or with
var_dump(htmlspecialchars($str1));
var_dump(htmlspecialchars($str2));
to inspect what's different, once found, paste here the different bits so we can figure out where the difference is from.
EDIT:
It's a &
encoded as &
notice that &
<-> &
are 4 chars different. what I think is going on is that you are sending a plain text emails, but entitizing the string, thus all &
are becoming &
.
Probably one is UTF-8 and one is iso-8859-1 or some other different encoding.
Try utf_decode() on one of them and see if it matches.
Adding onto @aularon answer, using:
var_dump(htmlspecialchars($str1));
var_dump(htmlspecialchars($str2));
exposed that it was indeed &
in one and not the other string.
To solve, I used htmlspecialchars_decode($str)
and that did the trick.
This might be a sneaky encoding issue, where one string has a combining char like å
stored as one unicode character, and the other string has it stored as two separate characters.
精彩评论