I have two same strings but one is having HTML entities and the other is its equivalent character.
$s1 = "‘Dragon’";
$s2 = "'Dragon'";
Is there any way I can detect that both strings are the same? I know that strcmp can not be used here and there is no mb_ function for comparing in PHP. Here is what I did but it is not working.
$coll = collator_create( 'en_US' );
$res = collator_compare( $coll, html_entity_decode($s1), $s2 );
if ($res === false) {
echo collator_get_error_message( $coll );
} else if( $res > 0 ) {
echo "s1 is greater than s2\n";
} else if( $res < 0 ) {
echo "s1 is less than s2\n";
} else {
echo "s1 is equal to s2\n";
}
CodePudding user response:
@saurabh, in your case you are dealing with the left single quote
special character. You can convert this to it's character by using the html_entity_decode
method:
$s1 = html_entity_decode($s1) // ‘Dragon’
Now you can compare the strings like you normally would do: $s1 == $s2
But since your $s2
doesn't have the left single quote
but the apostrophe
, your check will return false. So make sure the characters are the same.
$s1 = html_entity_decode("‘Dragon’"); // ‘Dragon’
$s2 = "'Dragon'"; // 'Dragon'
// false because ‘ is not the same as '
return $s1 == $s2; // false