Solved

if string contains a 1 or 2

Posted on 2012-12-27
3
238 Views
Last Modified: 2012-12-27
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
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38725799
if (strpos(theString, "1") || strpos(theString, "2")) {
    // do something
}

Open in new window

0
 

Expert Comment

by:gopakumarvr
ID: 38726131
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
 

Author Closing Comment

by:rgb192
ID: 38726148
thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question