php通过数组实现多条件查询实现方法(字符串分割)

yipeiwu_com6年前PHP代码库

复制代码 代码如下:

<?php
$keyword="asp php,jsp";
$keyword=str_replace(" "," ",$keyword);
$keyword=str_replace(" ",",",$keyword);
$keyarr=explode(',',$keyword);
for($index=0;$index<count($keyarr);$index++)
{
$whereSql .= " And (arc.title like '%$keyarr[$index]%' Or arc.keywords like '%$keyarr[$index]%') ";
}
echo $whereSql;


为了同时支持空格与逗号,需要提前替换为统一的逗号,就是先将空格都替换为逗号,然后再通过逗号分割字符串,然后循环拼接sql查询语句。

str_replace就是php中常用的字符串替换函数。
explode就是php中常用的字符串分割为数组的函数。

相关文章

优化PHP程序的方法小结

1. If a method c++an be static, declare it static. Speed improvement is by a factor of 4. 如果一...

PHP中的strtr函数使用介绍(str_replace)

strtr 有两种形式: string strtr ( string $str , string $from , string $to ) string strtr ( string $...

分享一个漂亮的php验证码类

本文实例为大家分享了一个漂亮的php验证码类,供大家参考,具体内容如下 //验证码类 class ValidateCode { private $charset = 'abcdef...

PHP中file_exists与is_file,is_dir的区别介绍

很显然file_exists是受了asp的影响,因为asp不但有fileExists还有folderExists,driverExists,那么PHP中file_exists是什么意思呢...

php+redis在实际项目中HTTP 500: Internal Server Error故障排除

问题描述 用户量快速增长,访问量在短时间内翻倍,由于前期容量规划做得比较好,硬件资源可以支撑,可是软件系统方面出现了大问题: 40% 的请求都会返回 HTTP 500: Internal...