0

I have a variable like this:

33,100,200

I need to detect if it contains a specific number, say

if(var contains '33'){
  do stuff
}

But it has to not work if say they didn't have 333 in the variable the above statement shouldn't validate the if statement.

Edit: This is a string not an array.

2
  • How is this variable stored? As an array? A string? Commented Feb 23, 2012 at 23:06
  • It's stored as a string not an array. Commented Feb 23, 2012 at 23:28

2 Answers 2

2

Either explode & in_array route, or preg_match('/(?<![0-9])33(?![0-9])/',$string) route, I prefer the first.

Sign up to request clarification or add additional context in comments.

1 Comment

The Preg Match Worked the best.
1

I dont quite understand the second part of your question, but this may be the code you're looking for:

if(strpos($var, '33') !== false) {
    // do stuff
}

Edit Oh, now I think i get what you're looking for

if(in_array('33', explode(',', $var)) {
    // do stuff
}

1 Comment

It's not an array, will that make a difference?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.