Page 1 of 1

How to match one long string phrase against another in PHP?

Posted: Mon Sep 15, 2008 11:50 pm
by sameerpanjwani
I am a bit confused on how to go about doing a match of one string against another. This is what I want to achieve:

String 1: You have selected tea, biscuits and an ice-cream.

String 2: Tea, Coffee, Biscuits, Ice-cream, Chocolates, Fruits

I want to match "String 1" against "String 2" and return "String 1" with all the matching words highlighted in yellow. So in this case, "String 1" would return with "tea", "biscuits" and "ice-cream" highlighted in yellow.

How would I go about this?

Re: How to match one long string phrase against another in PHP?

Posted: Tue Sep 16, 2008 3:54 am
by Ziq
This is the simplest example:

Code: Select all

 
<?
$data = 'You have selected tea, biscuits and an ice-cream';
$search = array('tea', 'biscuits');
 
foreach ($search as $v) $data = str_ireplace($v, '<span style="background-color: yellow;">'.$v.'</span>', $data);
 
echo $data;
?>
 

Re: How to match one long string phrase against another in PHP?

Posted: Tue Sep 16, 2008 4:02 am
by Ziq
But i don't know... If you need to search changed words (ice-cream, ice-creamS and e.t.c) it's harder than it.