views:

45

answers:

3

I have two strings:

$stringA = "1,2,3,4";
$stringB = "1,2,4,5";

I want to pick out the values from $stringB that are not in $stringA.

How can I do that?

A: 

explode() both arrays and use array_diff() to compute differential array.

killer_PL
A: 

http://pear.php.net/package/Text_Diff/

That should help you out ...

Nathan Loding
+8  A: 

A combination of explode and array_diff should get you there.

array_diff(explode(',', $stringB), explode(',', $stringA));
How can I display the results in a nice format instead of Array()?
John Norton
+1 Exactly what I was going to answer.
Daniel Vandersluis
If I understand the question correctly, you have to switch `$stringA` and `$stringB`. I think the OP wants all the values that are not in `$stringA`.
Felix Kling
There is var_dump, not sure what type of output you are looking for.
You are right they need to be switched, but how can I echo the results in a nice format instead of Array()?
John Norton
@John Norton: You could join them again, using `$str = implode(' ', $result);` or loop over the array and print the values.
Felix Kling
Output such as (words that are different):WordWordWord
John Norton
It works except that it displays the last value as not in the first string even though it is...
John Norton