简体   繁体   English

PHP的字符串比较不工作= =

[英]php string comparison not working on ==

i have question on string comparison i have 我对字符串比较有疑问

if($decryptedname == $plain)
{
echo "success <br/>";
echo $_SESSION['decryptedname'];
}

in a for loop to go through a text file, decryptedname contain a string "Lee" and plain contain the data in my text files and printing line by line, since both of it contain a field with the value "Lee" i assume it should match and print success as well as the $decryptedname but it's not, below is the copy and pasted result, where lee is $decryptedname and those others are echo by $plain thru a for loop 在for循环中,通过一个文本文件,decryptedname包含一个字符串“ Lee”,平原包含我的文本文件中的数据,并逐行打印,因为它们都包含一个值为“ Lee”的字段,我认为应该匹配和打印成功以及$ decryptedname,但不是,以下是复制和粘贴的结果,其中lee是$ decryptedname,而其他的则通过$循环通过for循环回显

Lee 背风处

Decrypted name is Lee 解密的名字是李

Decrypted email is mjlee181@hotmail.com 解密的电子邮件是mjlee181@hotmail.com

Decrypted message is testing 解密的邮件正在测试

There are chances for whitespace getting added when you load contents from a text file , so in that case always do a trim() and then do the comparison. 从文本文件加载内容时,很有可能会添加空白,因此在这种情况下,请始终执行trim()然后进行比较。

Like this.. 像这样..

if(trim($decryptedname) == trim($plain))
{
echo "success <br/>";
echo $_SESSION['decryptedname'];
}

or if you are trying to check if the string contains in another string... you should be doing stripos() instead. 或者,如果您要检查该字符串是否包含在另一个字符串中,则应stripos()

if(stripos($decryptedname,$plain)!==false)
{
    echo "success <br/>";
    echo $_SESSION['decryptedname'];
}
  <?php
        if (strcasecmp($decryptedname , $plain) == 0) {
            echo "success <br/>";
echo $_SESSION['decryptedname'];
    ?>

you can try this .May it help you 你可以试试看。可能对你有帮助

The above mentioned problem may be caused due to the '\\n' present at the end of the string. 上面提到的问题可能是由于字符串末尾存在'\\ n'引起的。 If the string "Lee" is present at the end of the line in the text file, it may automatically be appended with '\\n' in the end. 如果字符串“ Lee”出现在文本文件中该行的末尾,则可能会自动在末尾附加“ \\ n”。 So if you remove '\\n' from the end it might work fine. 因此,如果从末尾删除“ \\ n”,则可能会正常工作。 you can use the following function to do that: 您可以使用以下功能来做到这一点:

$plain=str_replace("\\r\\n","",$plain); $平原= str_replace函数( “\\ r \\ n” 个, “”,$平原);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM