Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.