Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two identical strings, yet different lengths. Trimmed, cleansed, etc

Tags:

php

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 they 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?

like image 608
Tarik Avatar asked Sep 04 '10 16:09

Tarik


2 Answers

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 &amp; notice that & <-> &amp; 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 &amp;.

like image 141
aularon Avatar answered Sep 29 '22 09:09

aularon


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.

like image 35
Iznogood Avatar answered Sep 29 '22 08:09

Iznogood