PHP Like a thing like MySQL Like, for if statement?

I need an if statement that uses the same thing as mysql something LIKE '%something%'

I want to build an if statement in php.

 if ($something is like %$somethingother%) 

Is it possible?

The reason I ask this question is because I do not want to change the MySQL command, it is a long page with many things on it, I do not want to create another function for it.

Let me know if it is possible, if possible, then how to do it.

+10
source share
6 answers

if ($ something like% $ somethingother%)

Is it possible?

not.

I do not want to change the MySQL command, this is a long page with many things on it

Use some good editor that supports regular expressions in search and replace, and turn it into something like:

 if(stripos($something, $somethingother) !== FALSE){ } 
+23
source

I know this question is not relevant, but I solved a similar problem :)

My decision:

 /** * SQL Like operator in PHP. * Returns TRUE if match else FALSE. * @param string $pattern * @param string $subject * @return bool */ function like_match($pattern, $subject) { $pattern = str_replace('%', '.*', preg_quote($pattern, '/')); return (bool) preg_match("/^{$pattern}$/i", $subject); } 

Examples:

 like_match('%uc%','Lucy'); //TRUE like_match('%cy', 'Lucy'); //TRUE like_match('lu%', 'Lucy'); //TRUE like_match('%lu', 'Lucy'); //FALSE like_match('cy%', 'Lucy'); //FALSE 
+14
source

look at strstr function

+4
source

Use the function, this search string in another line, for example: strstr , strpos , substr_count .

+2
source

But you will need to pass the line string, then it will work fine. Example strstr function:

 $myString = "Hello, world!"; echo strstr( $myString, "wor" ); // Displays 'world!' echo ( strstr( $myString, "xyz" ) ? "Yes" : "No" ); // Displays 'No' 
0
source

Use this function, which works the same as the SQL LIKE statement , but it will return a boolean, and you can create your own condition using another if statement

 function like($str, $searchTerm) { $searchTerm = strtolower($searchTerm); $str = strtolower($str); $pos = strpos($str, $searchTerm); if ($pos === false) return false; else return true; } $found = like('Apple', 'app'); //returns true $notFound = like('Apple', 'lep'); //returns false if($found){ // This will execute only when the text is like the desired string } 
0
source

All Articles