Question

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.?

Was it helpful?

Solution

// If not case sensitive

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

// If case sensitive

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

OTHER TIPS

<?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";
 }

?>
You can also use is_numeric function 

if (!is_numeric("test")) 
 {
 echo "Yes";
 } else {
 echo "No";
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top