【问题标题】:php - compare two stringsphp - 比较两个字符串
【发布时间】:2014-06-14 17:25:41
【问题描述】:

我有这部分 PHP 代码:

<?php
$dns = file_get_contents('./dns.txt');

$dns2 = 'serverquery://name:pass@example.com:10011/?server_port=9987&use_offline_as_virtual=1&no_query_clients=1';

$dns3 = 'serverquery://name:pass@' . $dns . ':10011/?server_port=9987&use_offline_as_virtual=1&no_query_clients=1';

if (strcmp($dns2, $dns3) !== 0) {
    echo '$dns2 is not equal to $dns3';
}

echo '<br><br>DNS<br>';
print $dns;
echo '<br><br>DNS2<br>';
print $dns2;
echo '<br><br>DNS3<br>';
print $dns3;
echo '<br><br><br>';

?>

文件 dns.txt 仅包含文本 example.com,开头或结尾没有任何空格

当我运行这段代码时,结果是:

$dns2 is not equal to $dns3

DNS
example.com

DNS2
serverquery://name:pass@example.com:10011/?server_port=9987&use_offline_as_virtual=1&no_query_clients=1

DNS3
serverquery://name:pass@example.com:10011/?server_port=9987&use_offline_as_virtual=1&no_query_clients=1

我正在尝试为 TeamSpeak 3 服务器创建服务器查询,它仅在我使用 $dns2 并且我希望它与 $dns3 一起使用时才有效

所以我的问题是:为什么 $dns2 不等于 $dns3

你能帮帮我吗?

【问题讨论】:

  • 使用var_dump()来验证文件的内容,看看它是否真的包含你认为的内容
  • 你是对的......但为什么它们不一样? DNS:(长度=14) DNS2:(长度=103) DNS3:(长度=106)
  • 另外看看实际输出,你就会得到答案。
  • 我正在尝试,但我看不出任何区别:/ string 'serverquery://name:pass@example.com:10011/?server_port=9987&use_offline_as_virtual=1&no_query_clients=1' (length=103 )字符串 'serverquery://name:pass@example.com:10011/?server_port=9987&use_offline_as_virtual=1&no_query_clients=1' (length=106)
  • 如果您在此处上传文件并且@fredirik 正确,您将看到three leading characters hexdump.pieterhordijk.com

标签: php string compare


【解决方案1】:

您的dns.txt 文件中有一些隐藏字符。确保它是 UTF-8 编码的没有 BOM。

UTF-8 BOM 是一个字节序列 (EF BB BF),因此需要三个字符的开销。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 2020-09-05
    • 2017-07-18
    相关资源
    最近更新 更多