1

By example:

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

the Result must be :

ResultString='apple,nature,grass'

3 Answers 3

2
$first = explode(',',$firstString);
$second = explode(',',$secondString);

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

$result = array_diff($first, $second);
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

You've got explode and implode backwards.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.