子串比较PHP

时间:2016-06-24 15:02:01

标签: php string compare substring string-comparison

为什么此代码不回显0

$email = "test@example.com";
$ending = "com";

$email = preg_replace('/[^A-Za-z0-9\-]/', '', $email);

echo substr_compare($email, $ending, strlen($ending)-strlen($email), strlen($ending));

我期待0 {,每documentation

1 个答案:

答案 0 :(得分:0)

如果您想检查它是否以" com"。

结尾

阅读本文:startsWith() and endsWith() functions in PHP


但是如果您只是想调试结果:

尝试将整数转换为字符串以正确输出结果。

echo (string)substr_compare($email, $ending, strlen($ending)-strlen($email), strlen($ending));

或做:

$result = substr_compare($email, $ending, strlen($ending)-strlen($email), strlen($ending));
var_dump($result); // or echo (string)$result;