wglenn01 Posted December 13, 2010 Share Posted December 13, 2010 Hey all, I have a quick question and I know the answer is probably pretty easy but i've been working on it a while now and can't figure out how to get this done. SOO the situation is. I am looping through an RSS feed and with each entry I want to search for a brand name within the title, and add a company logo beside the entry. I was playing with something like this but it doesn't work real well.. // Find Kokatat $find = "Kokatat"; if(strstr($string, $find)) { $icon = "koksm.jpg"; } else { $icon = ""; } I appreciate any input! Quote Link to comment https://forums.phpfreaks.com/topic/221550-string-search-question/ Share on other sites More sharing options...
AbraCadaver Posted December 13, 2010 Share Posted December 13, 2010 I don't know what "doesn't work real well" means, but I would try this: $find = "Kokatat"; if(stripos($string, $find) !== false) { $icon = "koksm.jpg"; } else { $icon = ""; } Quote Link to comment https://forums.phpfreaks.com/topic/221550-string-search-question/#findComment-1146848 Share on other sites More sharing options...
wglenn01 Posted December 13, 2010 Author Share Posted December 13, 2010 Well what I mean is, it works as a single search but when I add another, below it to search for a different name, if the first one succeeds and sets $icon, then the second doesn't succeed and sets $icon to empty. Does that make sense? I have 27 different names I need to loop through to see which one comes back with a match. Quote Link to comment https://forums.phpfreaks.com/topic/221550-string-search-question/#findComment-1146850 Share on other sites More sharing options...
wglenn01 Posted December 13, 2010 Author Share Posted December 13, 2010 Also I need it to be able to search for say "Primaloft" in this sentence, even though it has an R. PrimaLoft® Supports L.L. Bean’s 2010 Winter Sports Weekend in Freeport Quote Link to comment https://forums.phpfreaks.com/topic/221550-string-search-question/#findComment-1146855 Share on other sites More sharing options...
AbraCadaver Posted December 13, 2010 Share Posted December 13, 2010 Well what I mean is, it works as a single search but when I add another, below it to search for a different name, if the first one succeeds and sets $icon, then the second doesn't succeed and sets $icon to empty. Does that make sense? I have 27 different names I need to loop through to see which one comes back with a match. Several ways to do it. Here's one: $words = array('Kokatat'=>'koksm.jpg', 'Something'=>'something.jpg', 'Another'=>'123.jpg'); //etc... $icon = ''; foreach($words as $search => $icon) { if(stripos($string, $search) !== false) { break; } } echo $icon; Quote Link to comment https://forums.phpfreaks.com/topic/221550-string-search-question/#findComment-1146862 Share on other sites More sharing options...
wglenn01 Posted December 13, 2010 Author Share Posted December 13, 2010 Wow! Yay that works awesome, thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/221550-string-search-question/#findComment-1146868 Share on other sites More sharing options...
AbraCadaver Posted December 13, 2010 Share Posted December 13, 2010 Here's two other ways with some minor modifications: http://www.phpfreaks.com/forums/php-coding-help/search-string-for-any-match-to-a-list-of-words/msg1500104/#msg1500104 Quote Link to comment https://forums.phpfreaks.com/topic/221550-string-search-question/#findComment-1146870 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.