0

how string comparison is done in php?
i wan to copmpare two strings and result i want is the same letters in both strings?
any help plz

3
Contributors
5
Replies
7
Views
5 Years
Discussion Span
Last Post by baig772
0

The strcmp() function is binary safe and case-sensitive.

Example

<?php
echo strcmp("Hello world!","Hello world!");
?>

The output of the code above will be:
0

0

SO you want something like this?

Text1: Hello World
Text2: Hi words are my music

Hits: H,e,o,r,d

Do you need case-sensitivity or extended characters?

1
$text1 = "Hello this is!!! my time";
$text2 = "ThiHs is unbelievables";

$r1 = str_split(preg_replace("/[^a-zA-z]/","",$text1)); //make array of alphabetical chars only
$r2 = str_split(preg_replace("/[^a-zA-z]/","",$text2)); //ditto
$hits =array_unique(array_intersect($r1,$r2)); //get an array of common chars
sort($hits); //sort chars
$hits = implode(", ",$hits); //create a comma delimited list of chars
echo "Matches for \"$text1\" and \"$text2\" are: $hits"; //echo output

Maybe an easier method though.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.