Beauford Posted December 23, 2008 Share Posted December 23, 2008 I have a database that is going to have entries like the following: _1_2_3_4 _3_4_5_6 _1_3_5_6 _2_4_5_6 After I select from the database I want to check to see if a number is in each row i.e. _3. I am using the _ so that _3 and _33 are seperate entries. I have tried every string function in PHP and just can't seem to get what I want. Example of what I need: $ur=$db->query("SELECT urVOTED FROM user_rating"); while($vote=$db->fetch_row($ur)) { if _3 is found in $vote['urVOTED'] { do something } } I know this is probably simple, but it's 2 in the morning and I've been at this way to long. Thanks Link to comment https://forums.phpfreaks.com/topic/138137-need-help-with-string-search/ Share on other sites More sharing options...
Psycho Posted December 23, 2008 Share Posted December 23, 2008 Here is one way: if (in_array('3', explode('_', $vote['urVOTED'])) Link to comment https://forums.phpfreaks.com/topic/138137-need-help-with-string-search/#findComment-722085 Share on other sites More sharing options...
Mark Baker Posted December 23, 2008 Share Posted December 23, 2008 if (strpos('_3_', $vote['urVOTED'].'_') !== False) Link to comment https://forums.phpfreaks.com/topic/138137-need-help-with-string-search/#findComment-722099 Share on other sites More sharing options...
Beauford Posted December 23, 2008 Author Share Posted December 23, 2008 This, and a few variations of it, I tried and it does not find anything. If I put === False, it shows all of the entries don't contain the value, even though it does. if (strpos('_3_', $vote['urVOTED'].'_') !== False) This one seems to work. I tried similar to this, but not exactly as shown. if (in_array('3', explode('_', $vote['urVOTED'])) Thanks for the effort. Link to comment https://forums.phpfreaks.com/topic/138137-need-help-with-string-search/#findComment-722377 Share on other sites More sharing options...
premiso Posted December 23, 2008 Share Posted December 23, 2008 Why not just pull it out from the query? $ur=$db->query("SELECT urVOTED FROM user_rating WHERE urVoted like '%_3_%'"); while($vote=$db->fetch_row($ur)) { // should now only contain the votes with _3_ } If you want to do it in a loop for multiple cases, try this. $ur=$db->query("SELECT urVOTED FROM user_rating"); while($vote=$db->fetch_row($ur)) { if (strstr($vote['urVOTED'], '_3_') !== false) { echo '_3_ was found!'; } } Link to comment https://forums.phpfreaks.com/topic/138137-need-help-with-string-search/#findComment-722400 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.