php中compare函数,PHP collator_compare()用法及代码示例

collator_compare()函数是PHP中的内置函数,用于根据排序规则比较两个Unicode字符串。

用法:

程序风格:

int collator_compare( $coll, $str1, $str2 )

面向对象的样式:

int Collator::compare( $str1, $str2 )

参数:此函数接受上述和以下所述的三个参数:

$coll:此参数用作整理对象。它提供了比较功能,并支持适当的locale-sensitive排序顺序。

$str1:要比较的第一个字符串。

$str2:要比较的第二个字符串。

返回值:该函数返回比较结果,如下所示:

1:如果str1大于str2。

0:如果str1等于str2。

-1:如果str1小于str2。

错误:它返回False。

以下示例程序旨在说明PHP中的collator_compare()函数:

示例1:

// Declare variable and initialize it

$str1 = 'Geeks';

$str2 = 'geeks';

$coll = collator_create( 'en_US' );

// Compare both strings

$res = collator_compare( $coll, $str1, $str2 );

if ($res === false)

echo collator_get_error_message( $coll );

else if( $res > 0 )

echo $str1 . " is greater than " . $str2 . "\n";

else if( $res < 0 )

echo $str1 . " is less than " . $str2 . "\n";

else

echo $str1 . " is equal to " . $str2;

?>

输出:

Geeks is greater than geeks

示例2:

// Declare the variable and initialize it

$str1 = 'GeeksforGeeks';

$str2 = 'GeeksforGeeks';

$coll = collator_create( 'en_US' );

// Compare both strings

$res  = collator_compare( $coll, $str1, $str2 );

if ($res === false)

echo collator_get_error_message( $coll );

else if( $res > 0 )

echo $str1 . " is greater than " . $str2 . "\n";

else if( $res < 0 )

echo $str1 . " is less than " . $str2 . "\n";

else

echo $str1 . " is equal to " . $str2;

?>

输出:

GeeksforGeeks is equal to GeeksforGeeks

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值