sameerpanjwani Posted September 16, 2008 Share Posted September 16, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/124427-how-to-match-one-long-string-phrase-against-another-and-highlight-the-result/ Share on other sites More sharing options...
Maq Posted September 16, 2008 Share Posted September 16, 2008 Explode the string and compare each piece. If the piece matches then highlight it. Quote Link to comment https://forums.phpfreaks.com/topic/124427-how-to-match-one-long-string-phrase-against-another-and-highlight-the-result/#findComment-642568 Share on other sites More sharing options...
sameerpanjwani Posted September 16, 2008 Author Share Posted September 16, 2008 Would that be the most efficient way (exploding string 2 and then comparing it with each word in string 1) because string 2 can contain as many as 60-70 words and as much for string 1. Would it take a lot of processing time if the strings became that long? Quote Link to comment https://forums.phpfreaks.com/topic/124427-how-to-match-one-long-string-phrase-against-another-and-highlight-the-result/#findComment-642579 Share on other sites More sharing options...
sameerpanjwani Posted September 16, 2008 Author Share Posted September 16, 2008 Ok I've made a mistake, string 2 is not necessarily in such an easy to decode format like mentioned above, it could be more like: String 2: The items available are "Drinks - tea, coffee"; "Snacks - Biscuits", "Dessert - Ice-cream" Quote Link to comment https://forums.phpfreaks.com/topic/124427-how-to-match-one-long-string-phrase-against-another-and-highlight-the-result/#findComment-642582 Share on other sites More sharing options...
Maq Posted September 16, 2008 Share Posted September 16, 2008 No. The string examples you provided aren't very long. Now, if you're processing 300,000 records of this length it could take a while. Quote Link to comment https://forums.phpfreaks.com/topic/124427-how-to-match-one-long-string-phrase-against-another-and-highlight-the-result/#findComment-642590 Share on other sites More sharing options...
sameerpanjwani Posted September 16, 2008 Author Share Posted September 16, 2008 How would I explode the string without the quotes and commas being returned as part of the individual words returned. Quote Link to comment https://forums.phpfreaks.com/topic/124427-how-to-match-one-long-string-phrase-against-another-and-highlight-the-result/#findComment-642602 Share on other sites More sharing options...
JasonLewis Posted September 16, 2008 Share Posted September 16, 2008 Like this? $str1 = "The items available are \"Drinks - tea, coffee\", \"Snacks - Biscuits\", \"Dessert - Ice-cream\""; //These are words that you want highlighted $str2 = "Tea, Coffee, Biscuits, Ice-cream"; //Build the matches $arr = explode(",", $str2); $match = array(); foreach($arr as $highlight){ $match[] = "/{$highlight}/is"; } echo preg_replace($match, "<span style='background-color:yellow;'>\\0</span>", $str1); Quote Link to comment https://forums.phpfreaks.com/topic/124427-how-to-match-one-long-string-phrase-against-another-and-highlight-the-result/#findComment-642635 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.