Rene
Rene

Reputation: 59

compare two strings of same content

i want to compare two strings if there is the same content (text).

I have these two strings

String1 - $preset->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)

String2 - $_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)

so i trie to compare them in this ways

first

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

for me bothe strings are the same but the code above said its different

so i trie to get more infos

$sim = similar_text($_data['message'], $preset->message, $perc);
echo "similarity: $sim ($perc %) - ";

As Result i get 99.428571428571 %

So How i can chek in a easy and working way if the text is already the same or not? My tries seems not the right way for do that...

Thanks in advance for helping me at this.

Upvotes: 0

Views: 60

Answers (2)

Chayan
Chayan

Reputation: 614

$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";

Upvotes: 0

Tamilvanan
Tamilvanan

Reputation: 718

You have to trim the white spaces, tabs and line breaks to compare this kind of lengthy strings. Try the below code.

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

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

Upvotes: 1

Related Questions