php实战案例记录(13)关键词包含空格的并且搜索条件

要在 PHP 中搜索包含空格的关键词,并使用 MySQL 语句进行查询,你可以使用 MySQL 的 LIKE 运算符和 % 通配符来实现。

MySql拼接语句

php 复制代码
$condition = '';
if (isset($word) && $word != '')
{
    $keyworksqland = '';
    $wordlist = str_replace("+", " ", $word);
    $wordlist = str_replace(" ", " ", $wordlist);
    $wordlistx = explode(" ", $wordlist);
    $condition .= " and ( ";
    for ($k = 0; $k < count($wordlistx); $k++) {
        if ($k > 0) {
            $keyworksqland = " and ";
        }
        $condition .= $keyworksqland . "  name like '%" . addslashes($wordlistx[$k]) . "%'";
    }
    $condition .= " )";
}

mysql运行结果

and ( name like '%源代码%' and name like '%数据%' and name like '%UI%' )


@漏刻有时

相关推荐
石山代码6 小时前
ArrayList / HashMap / ConcurrentHashMap
java·开发语言
程序大视界6 小时前
【Python系列课程】Python正则表达式(下):环视、命名分组与日志实战
开发语言·python·正则表达式
针叶6 小时前
Google Play加固保护导致的崩溃
android·安全·google
枫叶v.7 小时前
Agent 分层存储架构设计:从记忆方法到中间件选型
开发语言·python
sleven fung8 小时前
MinerU与BabelDOC与KTransformers与OpenAI API库
开发语言·python·ai·langchain
萤萤七悬8 小时前
【Python笔记】AI帮实现CLI工具-使用argparse.ArgumentParser接收命令参数
开发语言·笔记·python
iCxhust8 小时前
C# 命令行指令 查看二进制文件
开发语言·单片机·嵌入式硬件·c#·proteus·微机原理·8088单板机
csdn_aspnet8 小时前
Java 霍尔分区算法(Hoare‘s Partition Algorithm)
java·开发语言·算法
执明wa8 小时前
Android Studio 项目目录结构全方位详解
android·ide·android studio
诸葛务农9 小时前
道路行驶条件下电动汽车永磁电机的有效使用寿命及永磁体的失效和回收再利用(下)
java·开发语言·算法