Home  >  Article  >  Backend Development  >  PHP implements multi-condition query implementation method through array (string splitting)_PHP tutorial

PHP implements multi-condition query implementation method through array (string splitting)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:30:19903browse

Copy code The code is as follows:

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


In order to support both spaces and commas, It needs to be replaced with a unified comma in advance, that is, first replace all spaces with commas, then split the string by commas, and then splice the SQL query statement in a loop.

str_replace is the commonly used string replacement function in php.
explode is a function commonly used in PHP to split strings into arrays.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/765722.htmlTechArticleCopy the code as follows: ?php $keyword="asp php,jsp"; $keyword=str_replace(" " ," ",$keyword); $keyword=str_replace(" ",",",$keyword); $keyarr=explode(',',$keyword); for($index=0;...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn