我有兩個相同的字符串,但一個是 HTML 實體,另一個是它的等效字符。
$s1 = "‘Dragon’";
$s2 = "'Dragon'";
有什麼辦法可以檢測到兩個字符串是否相同?我知道這裡不能使用strcmp並且在 PHP 中沒有用於比較的mb_函數。這是我所做的,但它不起作用。
$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";
}
@saurabh,在您的情況下,您正在處理left single quote
特殊字符。您可以使用以下html_entity_decode
方法將其轉換為它的字符:
$s1 = html_entity_decode($s1) // ‘Dragon’
現在您可以像往常一樣比較字符串: $s1 == $s2
但是由於您$s2
沒有left single quote
但是apostrophe
,您的支票將返回false。因此,請確保字符相同。
$s1 = html_entity_decode("‘Dragon’"); // ‘Dragon’
$s2 = "'Dragon'"; // 'Dragon'
// false because ‘ is not the same as '
return $s1 == $s2; // false
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句