Is there any solution to have a string comparison function in PHP identical to JavaScript's string.localeCompare()
?
My goal is to make a hasher that can be applied over simple objects in PHP and in JS as well. The property ordering can be solved based on this question: Sorting JavaScript Object by property value
I make a list of property key and value tuplets and I order the list by the keys. (Note that this can work recursively too.) But I'm a bit afraid of string representations and locales, that they might be tricky. If the property keys contain some fancy characters then the sorting may be different in PHP and JS side, resulting in different hashes.
Try the following in PHP, and make a similar comparison in JS.
echo strcmp('e', 'é')."\n";
echo strcmp('e', 'ě')."\n";
echo strcmp('é', 'ě')."\n";
echo strcmp('e', 'f')."\n";
echo strcmp('é', 'f')."\n"; // This will differ
echo strcmp('ě', 'f')."\n"; // This will differ
Main question: how can I perform identical string comparison in PHP and JS?
Side question: Other ideas for object hashing in PHP and JS?