Jump to content

String search question


wglenn01

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/221550-string-search-question/
Share on other sites

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.

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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.