Jump to content


Photo

Search Whole Words in String


  • Please log in to reply
3 replies to this topic

#1 dtyson2000

dtyson2000
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 18 June 2006 - 09:13 PM

I'm relatively new to this so I apologize in advance.

I would like to be able to search a string for a word(s) and if that string has the word in it, set a variable to be referenced later in the html.

For example: if the word "apple" is in a string (which has come from the database), then set $variable to equal "apple". Then later in the html I will have something like "img src=$variable.jpg", which will then place a picture of an apple on the resulting page.

I just can't get the search part down. Thanks, in advance!



EDIT: Thanks to akitchin, This is now solved!

#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 18 June 2006 - 11:31 PM

take a look in the manual at either strstr(), or ereg(), or any of the other regular expression functions (preg_match()). it does precisely what you're after (checks in a string for another string).

#3 dtyson2000

dtyson2000
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 19 June 2006 - 01:16 AM

OK, thanks!

I'll check it out and let you know what happens.

Thanks again!

#4 dtyson2000

dtyson2000
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 19 June 2006 - 03:15 AM

Hey it worked!

Here's what I did:

// search string for keywords
if ( strstr("$string_to_be_searched", "search terms") ) {
$variable = "<img src='path/image.jpg'>";
}
elseif ( strstr("$string_to_be_searched", "other search terms") ) {
$variable = "<img src='path/different_image1.jpg'><img src='path/different_image2.jpg'>";
}
else
{
$variable = "";
}
// end code block

Man I love this stuff! Might not be lined up like the pros do it but it does the trick for an amateur. Thanks again for your advice!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users