3
$textone = "pate"; //$_GET
$texttwo = "tape";
$texttre = "tapp";

if ($textone ??? $texttwo) {
echo "The two strings contain the same letters";
}
if ($textone ??? $texttre) {
echo "The two strings NOT contain the same letters";
}

What if statement am I looking for?

Mat
  • 202,337
  • 40
  • 393
  • 406
faq
  • 2,965
  • 5
  • 27
  • 35
  • Do you want to know if two strings contain the same characters, in the same order (the strings are the same), or do you want to know if any character from one string is in the other string? Can you show the expected result for the strings you have provided? – Mike Jul 24 '11 at 15:58
  • i want to know if all character from one string is in the other string – faq Jul 24 '11 at 16:00
  • How about `tap` and `tapp` - what would you expect the result to be? – Mike Jul 24 '11 at 16:14

2 Answers2

13

I suppose a solution could be to, considering the two following variables :

$textone = "pate";
$texttwo = "tape";


1. First, split the strings, to get two arrays of letters :

$arr1 = preg_split('//', $textone, -1, PREG_SPLIT_NO_EMPTY);
$arr2 = preg_split('//', $texttwo, -1, PREG_SPLIT_NO_EMPTY);

Note that, as pointed out by @Mike in his comment, instead of using preg_split() like I first did, for such a situation, one would be better off using str_split() :

$arr1 = str_split($textone);
$arr2 = str_split($texttwo);


2. Then, sort those array, so the letters are in alphabetical order :

sort($arr1);
sort($arr2);


3. After that, implode the arrays, to create words where all letters are in alphabetical order :

$text1Sorted = implode('', $arr1);
$text2Sorted = implode('', $arr2);


4. And, finally, compare those two words :

if ($text1Sorted == $text2Sorted) {
    echo "$text1Sorted == $text2Sorted";
}
else {
    echo "$text1Sorted != $text2Sorted";
}



Turning this idea into a comparison function would give you the following portion of code :

function compare($textone, $texttwo) {
    $arr1 = str_split($textone);
    $arr2 = str_split($texttwo);

    sort($arr1);
    sort($arr2);

    $text1Sorted = implode('', $arr1);
    $text2Sorted = implode('', $arr2);

    if ($text1Sorted == $text2Sorted) {
        echo "$text1Sorted == $text2Sorted<br />";
    }
    else {
        echo "$text1Sorted != $text2Sorted<br />";
    }
}


And calling that function on your two words :

compare("pate", "tape");
compare("pate", "tapp");

Would get you the following result :

aept == aept
aept != appt
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • what the hell? what is difference between === and yours? – genesis Jul 24 '11 at 16:03
  • @genesis `'ab'` and `'ba'` contain the same letters, so my `compare()` function says they are the same. On the other hand, `'ab'` and `'ba'` are not equal, so `var_dump("ab" === "ba");` will get you `boolean false` – Pascal MARTIN Jul 24 '11 at 16:05
  • @Mike shame on me, I had totally forgotten about that function ; thanks for your comment, I will edit my answer to point out that str_split could be used to :-) – Pascal MARTIN Jul 24 '11 at 16:15
-1

use === and !==

if ($textone === $texttwo) {
    echo "The two strings contain the same letters";
}else{
    echo "The two strings NOT contain the same letters";
}

or

if ($textone === $texttwo) {
    echo "The two strings contain the same letters";
}

if ($textone !== $texttwo) {
    echo "The two strings NOT contain the same letters";
}
genesis
  • 50,477
  • 20
  • 96
  • 125