Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Like thing similar to MySQL Like, for if statement?

I want an if statement that uses same thingy like mysql something LIKE '%something%'

I want to build an if statement in php.

if ($something is like %$somethingother%)

Is it possible?

The reason for me asking this question is that I don't want to change the MySQL command, it's a long page with many stuff on it, I don't want to build a different function for this.

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

like image 872
friendishan Avatar asked Feb 06 '11 08:02

friendishan


People also ask

How use like operator in if condition in PHP?

PHP stands for hypertext preprocessor. It is used as a server-side scripting language and can be used to connect with MySQL server with xampp tool. MySQL is a query language for managing databases. The LIKE operator in SQL is used in a WHERE clause to search for a specified pattern in a column.

Can we use like in if statement?

Sample 43303: Using the equivalent of CONTAINS and LIKE in an IF statement. Both the IF and WHERE statements can be used to subset data. The LIKE operator in a WHERE clause matches patterns in words. To get the equivalent result in an IF statement, the '=:' operator can be used.

What is like in PHP?

In this article I will explain the MySQL "Like" operator in PHP. The MySQL Like operator is based on pattern matching and commonly used to select data based on pattern matching. The LIKE operator allows you to select data from a MySQL database table and therefore this operator is used in the WHERE clause.

Is like used in MySQL?

The MySQL LIKE Operator The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.


6 Answers

if ($something is like %$somethingother%)

Is it possible?

no.

I don't want to change the MySQL command, it's a long page with many stuff on it

Use some good editor, that supports regular expressions in find & replace, and turn it to something like:

if(stripos($something, $somethingother) !== FALSE){

}
like image 149
dev-null-dweller Avatar answered Oct 05 '22 13:10

dev-null-dweller


I know, this question isn't actual but I've solved similar problem :)

My solution:

/**
 * 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
like image 23
Petr Bugyík Avatar answered Oct 05 '22 11:10

Petr Bugyík


look on strstr function

like image 32
Haim Evgi Avatar answered Oct 05 '22 13:10

Haim Evgi


Use this function which works same like SQL LIKE operator but it will return boolean value and you can make your own condition with one more 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
}
like image 34
Bairavan Avatar answered Oct 05 '22 12:10

Bairavan


Use function, that search string in another string like: strstr, strpos, substr_count.

like image 24
Radek Benkel Avatar answered Oct 05 '22 11:10

Radek Benkel


strpos() is not working for so i have to use this preg_match()

$a = 'How are you?';

if (preg_match('/\bare\b/', $a)) {
    echo 'true';
}

like in this e.g i am matching with word "are" hope for someone it will be helpful

like image 32
Asad Ullah Avatar answered Oct 05 '22 12:10

Asad Ullah