• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

if string contains a 1 or 2

if string contains a
1
or
2

do something

for example
hello1
contains a 1

42
contains a 2

34
nothing

32675
contains a 2
0
rgb192
Asked:
rgb192
1 Solution
 
käµfm³d 👽Commented:
if (strpos(theString, "1") || strpos(theString, "2")) {
    // do something
}

Open in new window

0
 
gopakumarvrCommented:
if(  strpos($mystring, '1')  )
{
    echo "Contains 1";
}
else if(  strpos($mystring, '2')  )
{
    echo "Contains 2";
}
else
{
   echo "Does not contain 1 or 2";
}

Check out these functions also
stristr() - Case-insensitive strstr
strrchr() - Find the last occurrence of a character in a string
strpos()  - Find the position of the first occurrence of a substring in a string
strpbrk() - Search a string for any of a set of characters
preg_match() - RE match..
0
 
rgb192Author Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now