PHP Like 类似于 MySQL Like 的东西,对于 if 语句?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4912294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:15:04  来源:igfitidea点击:

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

phpmysqlif-statement

提问by friendishan

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

我想要一个使用与 mysql 相同的东西的 if 语句 something LIKE '%something%'

I want to build an if statement in php.

我想在 php 中构建一个 if 语句。

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.

我问这个问题的原因是我不想更改 MySQL 命令,这是一个很长的页面,上面有很多东西,我不想为此构建不同的功能。

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

让我知道这是否可能,如果可能,那么如何去做。

回答by dev-null-dweller

if ($something is like %$somethingother%)

Is it possible?

if ($something 就像 %$somethingother%)

是否可以?

no.

不。

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

我不想更改 MySQL 命令,这是一个很长的页面,上面有很多东西

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

使用一些好的编辑器,它支持查找和替换中的正则表达式,并将其转换为:

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

}

回答by Petr Bugyík

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

回答by Haim Evgi

look on strstrfunction

查看strstr函数

回答by Radek Benkel

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

使用函数,在另一个字符串中搜索字符串,如:strstr, strpos, substr_count

回答by Asad Ullah

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

strpos() 不起作用所以我必须使用这个 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

就像在这个例子中,我匹配单词“are”希望对某人有帮助

回答by Reese

If you have access to a MySQL server, send a query like this with MySQLi:

如果您有权访问 MySQL 服务器,请使用 MySQLi 发送如下查询:

$SQL="select case when '$Value' like '$Pattern' then 'True' else 'False' end as Result";
$Result=$MySQLi->query($SQL)->fetch_all(MYSQLI_ASSOC)[0]['Result'];

Result will be a string containing True or False. Let PHP do what it's good for and use SQL for likes.

结果将是一个包含 True 或 False 的字符串。让 PHP 为所欲为,并为喜欢而使用 SQL。

回答by Ehsan

But you will have to give lowercase string then it will work fine. Example of strstr function:

但是您必须提供小写字符串,然后它才能正常工作。strstr 函数示例:

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

回答by Bairu

Use this function which works same like SQL LIKEoperator but it will return boolean value and you can make your own condition with one more if statement

使用此函数,其工作方式与 SQL LIKE运算符相同,但它会返回布尔值,您可以再使用一个 if 语句来创建自己的条件

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
}