比较两个相同内容的字符串

时间:2018-09-27 05:42:26

标签: php

如果内容(文本)相同,我想比较两个字符串。

我有这两个字符串

字符串1-$ preset->消息

rebracket = {91: 123, 93: 125}
a = '[100:0.345,123:0.34,145:0.86]'
literal_eval(a.translate(rebracket))

字符串2-$ _data ['message']

Dear {contact_firstname} {contact_lastname}

Thank you for registering on the {companyname} CRM System.

We just wanted to say welcome.

Please contact us if you need any help.

Click here to view your profile: {crm_url}

Kind Regards,
{email_signature}

(This is an automated email, so please don't reply to this email address)

所以我想以此方式比较它们

第一

Dear {contact_firstname} {contact_lastname}

Thank you for registering on the {companyname} CRM System.

We just wanted to say welcome.

Please contact us if you need any help.

Click here to view your profile: {crm_url}

Kind Regards, {email_signature}

(This is an automated email, so please don't reply to this email address)

对我来说,两个字符串都是相同的,但是上面的代码表示不同

所以我想获得更多信息

if ($_data['message']!=$preset->message){...}

结果我得到99.428571428571%

所以,如果文本已经相同或不同,我该如何以一种简单而又有效的方式进行检查?我的尝试似乎不是这样做的正确方法...

在此先感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您必须修剪空格,制表符和换行符才能比较这种冗长的字符串。尝试下面的代码。

$str1 = preg_replace("/\s+/", " ", $str1);
$str2 = preg_replace("/\s+/", " ", $str2);

if($str1 == $str2) {
    echo '1';
} else {
    echo '0';
}

答案 1 :(得分:0)

$str1 = "Dear {contact_firstname} {contact_lastname}

Thank you for registering on the {companyname} CRM System.

We just wanted to say welcome.

Please contact us if you need any help.

Click here to view your profile: {crm_url}

Kind Regards,
{email_signature}

(This is an automated email, so please don't reply to this email address)";


$str2 = "Dear {contact_firstname} {contact_lastname}

Thank you for registering on the {companyname} CRM System.

We just wanted to say welcome.

Please contact us if you need any help.

Click here to view your profile: {crm_url}

Kind Regards, {email_signature}

(This is an automated email, so please don't reply to this email address)";



echo strcmp($str1, $str2) ? "Equal" : "Not Equal";