0

I would like to have if condition which can validate PHP variable has value like string

 $string = "language English"
 $lan = "ENG"
 //So $lan has value "ENG" which is in string 
 if($lan LIKE $string )
 {
         // DO some thing.
 }

Is there way to check this? or any other ways, good examples, good practice.?

2
  • 6
    You are looking for stristr or stripos. Commented May 8, 2014 at 13:20
  • @mario Why do people like you comment rather than answer when you clearly know the answer? lol just wondering :) Commented May 8, 2014 at 13:27

3 Answers 3

3
// If not case sensitive

if (strpos($string,$lan) !== false) {
    echo 'true';
}

// If case sensitive

if (stripos($string,$lan) !== false) {
    echo 'true';
}
Sign up to request clarification or add additional context in comments.

1 Comment

stripos otherwise no match.
1
<?php

 $string = "language English";
 $lan = "ENG";

 $eng = '/'.$lan.'/i'; // 'i' indicates a case-insensitive search
 $check=preg_match($eng, $string);

 if($check)
 {
 echo "Match Found";    
 }
 else
 {
 echo "Match not found";
 }

?>

Comments

0
You can also use is_numeric function 

if (!is_numeric("test")) 
 {
 echo "Yes";
 } else {
 echo "No";
 }

3 Comments

this doesn't answer the question
if(!is_numeric("ENG")) {// // DO some thing.}
OP want to do something if the value eng appears in another string language English. Not when the value is a non-numeric

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.