Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string compare and join

Status
Not open for further replies.

kaancho12

Technical User
Feb 22, 2005
191
hi,
i need to compare strings which are similar to this:
food>fruit>red>cherry
food>fruit>red>apple
is there a way to compare these strings and only copy the difference in the second one and add it to the first?
for example after comparing i need the string to be:
foor>fruit>red>cherry^apple

i am not sure what php function to use.
thanks
ko12
 
I would turn each string into an array, do an array_diff and then recreate the first string if necessary.

Code:
<?php
$str1 = 'food>fruit>red>cherry';
$str2 = 'food>fruit>red>apple';

$arr1 = explode('>',$str1);
$arr2 = explode('>',$str2);

$arr_diffs = array_diff($arr2,$arr1);

if (!empty($arr_diffs)) $str1 .= '^' . implode('^',$arr_diffs);

echo $str1;
?>

Typed without too much checking. YMMV.

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top