【问题标题】:Substring Compare PHP子串比较 PHP
【发布时间】:2016-10-27 06:05:28
【问题描述】:

为什么这段代码没有回显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

【问题讨论】:

  • strlen($ending)-strlen($email) 用你的例子写下来,输入实数,然后计算一下,看看你得到了什么偏移量以及从哪里开始比较。
  • @Rizier123 哦! -11,我想我只需要做-strlen($ending)
  • 有时当您有一个小代码示例时,将其写下来并输入实数/值会有所帮助。
  • @num8er 它可能是相似的,因为它显示了另一种方法,但我认为 OP 在这里问他为什么在他的具体示例中没有 0 作为输出。
  • @Rizier123 作者想比较它以“com”结尾。请参阅标题:“子字符串比较 PHP”。并希望通过回显来调试结果。我的事情 var_dump() 会返回预期的结果。

标签: php string compare substring string-comparison


【解决方案1】:

如果你想检查它是否以“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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    相关资源
    最近更新 更多