Jump to content

strpos quesiton


shackwm60

Recommended Posts

This will be an easy one for you guys...

 

One of the fields in a MySQL database is a varchar() that was populated using a checkbox method and can contain more than one response. in this case i have blue, yellow, green and want to check if the field contains blue before echoing the statement to the screen. How would i use strpos in this case? ive tried a few thigns and get syntax error. The below of course works if the field ONLY contains blue.

 if (($row['colorsyoulike']) =='blue') {
   echo "<tr>";
   echo "<td>Blue is an available Color!</td>";
 
}

 

thanks.

Link to comment
https://forums.phpfreaks.com/topic/293813-strpos-quesiton/
Share on other sites

if (strpos($row['colorsyoulike'], 'blue') !== FALSE)
{
  //$row['colorsyoulike'] contained 'blue' somewhere in the string
}

strpos() will return boolean FALSE if the string isn't contained in the search string.

 

However, it would probably be better to use stripos() which is case-insensitive, so it will match blue, BLUE, Blue, BlUe etc.

Link to comment
https://forums.phpfreaks.com/topic/293813-strpos-quesiton/#findComment-1502426
Share on other sites

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.