tags:

views:

55

answers:

3

By example:

FirstString='apple,gold,nature,grass,class'
SecondString='gold,class'

the Result must be :

ResultString='apple,nature,grass'
A: 

I'm sure there is a function that can do it but you could always break up the strings and do a foreach on each one and do some string compares and build a new string. You could also break apart the second string and create a regular expression and do a preg_replace to replace the values in the string.

A: 

Hello Beharng... this is the easy way (for sure there must be more efficient ones):

First of all, you may want to separate those coma-separated strings and put them into an array (using the explode function):

$array1 = explode(',' $firstString);
$array2 = explode(',' $secondString);

Then, you can loop the first array and check whether it contents words of the second one using the in_array function (if so, delete it using the unset function):

// loop
foreach( $arra1 as $index => $value){
    if( in_array ( $value , $array2 ) )
        unset($array1[$index]); // delete that word from the array
}

Finally, you can create a new string with the result using the implode function:

$result = implode(',' , $array1);

That's it :D

Cristian
You've got explode and implode backwards.
Fletcher Moore
Yes... fixed. Thank you
Cristian
+2  A: 
$first = explode(',',$firstString);
$second = explode(',',$secondString);

Now you have arrays and you can do whatever you want with them.

$result = array_diff($first, $second);
Fletcher Moore