-1

I googled this question I can't to find the exact solution...

I have 2 variables...

$s1 = "ABC"; //or "BC"
$s2 = "BC"; //or "Bangalore"

I have to compare $s1 and $s2 and give the output as letters which is not present in $s2 eg : "A" // or"C"

Like that I have to compare $s2 and $s1 and give the output as letters which is not present in $s1 eg : null // or"angalore"

What I tried.. I spit the strings to array... Using nested for loop to find the non matched letters... I wrote code more than 35 lines.. But no result :( Please help me ......

rynhe
  • 2,509
  • 1
  • 21
  • 27
  • Do you really want to compare single letters? In the example you give the result could never be "Angalore", because it would only return single letters. – eevaa Jan 04 '14 at 13:29
  • 1
    let's say ... "ABC" and "Bangalore" are Sets (Set theory of Math), then you want "ABC" minus "Bangalore" and "Bangalore" minus "ABC". is it what you want? – Tun Zarni Kyaw Jan 04 '14 at 13:31
  • http://stackoverflow.com/questions/7475437/find-first-character-that-is-different-between-two-strings –  Jan 04 '14 at 13:34
  • `$s1 = "ABC";` or `$s1 = "BC"` just i gave two value for example.... please take any one `$s1 = "ABC";` `$s2 = "BC";` or `$s1 = "BC";` `$s2 = "Bangalore";` – rynhe Jan 04 '14 at 13:36
  • @Rotherford I already saw that post.. that is different – rynhe Jan 04 '14 at 13:38

5 Answers5

3
echo str_ireplace(str_split($s2), "", $s1); // output: A
revo
  • 47,783
  • 14
  • 74
  • 117
2

You can use array_diff() here:

function str_compare($str1, $str2)
{
    $str1chars = str_split($str1);
    $str2chars = str_split($str2);
    $diff = array_diff($str1chars, $str2chars)
    return implode($diff);
}

By calling the function as follows:

$diffchars = str_compare('ABC', 'BC');

You will receive a string containing the characters that do not appear in both strings. In this example, it'll be A, because that character appears in $str1, but not in $str2.

BenM
  • 52,573
  • 26
  • 113
  • 168
2

Use array_diff():

function str_diff($str1, $str2) {
    $arr1 = str_split($str1);
    $arr2 = str_split($str2);
    $diff = array_diff($arr1, $arr2);
    return implode($diff);
}

Usage:

echo str_diff('BC', 'Bangalore'); // => C
echo str_diff('ABC', 'BC');       // => A
BenM
  • 52,573
  • 26
  • 113
  • 168
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
2

You can use str_split and array_diff like :

<?php
$s1 = 'abcedf';
$s2 = 'xzcedf5460gf';

print_r(array_diff(str_split($s1), str_split($s2)));
OneOfOne
  • 95,033
  • 20
  • 184
  • 185
2

Ok to do this

$str1s = "abc";
$str2s = "BCd";


function findNot($str1, $str2, $asArray = false){

    $returnValue = array_diff(array_unique(str_split(strtolower($str1))), array_unique(str_split(strtolower($str2))));

    if($asArray == false){

        return implode($returnValue);

    }else{

        return $returnValue;

    }

}

echo findNot($str1s, $str2s); //gives a string
echo findNot($str1s, $str2s, true); //gives array of characters 

This allows you to return as either array or string.

GrahamTheDev
  • 22,724
  • 2
  • 32
  • 64