Jump to content

Need help with string search.


Beauford

Recommended Posts

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

 

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.

 

 

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!';
     }
}

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.