Jump to content

strpos and numbers?


galvin

Recommended Posts

Say you have a simple string that has just numbers and commas, like...

 

$numbers="12,18,24,32,108,190,4002"

 

Now say I want to check if the number "8" is in that string.  If I use the following, it will return true because 8 is in "18", but I'd want it to return false since the individual number 8 is not there.

 

$search=strpos($numbers,;
//returns true

 

What is the best way to get around this?  Break the string into an array first then use in_array, or something else?

Link to comment
https://forums.phpfreaks.com/topic/268575-strpos-and-numbers/
Share on other sites

The other more complicated option is to check if 8, is first, ,8 is last, or ,8, exists. The first way is easier and smarter.

 

You could just do:

$search = str_pos(",{$numbers},", ',8,');

 

I'd probably go with the explode/in_array solution though, it's a bit more proper.

Link to comment
https://forums.phpfreaks.com/topic/268575-strpos-and-numbers/#findComment-1379427
Share on other sites

The other more complicated option is to check if 8, is first, ,8 is last, or ,8, exists. The first way is easier and smarter.

 

You could just do:

$search = str_pos(",{$numbers},", ',8,');

 

I'd probably go with the explode/in_array solution though, it's a bit more proper.

 

Touche!

Link to comment
https://forums.phpfreaks.com/topic/268575-strpos-and-numbers/#findComment-1379429
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.