Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP和mysql忽略数据库中存在的特殊字符_Php_Mysql_Sql_Regex - Fatal编程技术网

PHP和mysql忽略数据库中存在的特殊字符

PHP和mysql忽略数据库中存在的特殊字符,php,mysql,sql,regex,Php,Mysql,Sql,Regex,在这一点上有一点帮助,这里是它的细节 [Products] id int name text category color 问题在于颜色字段的值,示例值为: GOLDRED 金红色 金色/红色 蓝色/绿色-红色 白金-黄色/橙色 我可以使用一个基本函数来清理搜索查询,比如这个示例 "select * from products where color=".cleanstring($stringval)." limit 1"; function cleanstring($var) {

在这一点上有一点帮助,这里是它的细节

[Products]
id int
name text
category
color
问题在于颜色字段的值,示例值为:

  • GOLDRED
  • 金红色
  • 金色/红色
  • 蓝色/绿色-红色
  • 白金-黄色/橙色
我可以使用一个基本函数来清理搜索查询,比如这个示例

"select * from products where color=".cleanstring($stringval)." limit 1";

function cleanstring($var) {
    $newtext = $var;
    $newtext = preg_replace("/[^a-zA-Z0-9\s]/", "", $newtext);
    $newtext = str_replace(" ", "", $newtext);
    $newtext = strtoupper($newtext);
    return $newtext;    
}
问题在于内容。数千条记录在使用命名约定时没有任何形式的标准

我想选择那些值与我的
cleanstring()
类似的clean记录

例如:

Query = GOLDRED
可以选择

  • 金红色
  • 金红色
  • GOLDRED
  • 金色/红色
  • GOLDRED
你能推荐什么解决方案吗?代码是用PHP/MySQL编写的

"select * from products where 1".cleanstring($stringval);

function cleanstring($var) {
$color_list = array('GOLD','RED','GREEN','WHITE');

$sql_where='';
foreach( $color_list AS $v){
    if(strpos($var, $v)!==false){
    $sql_where .=" AND color LIKE '%{$v}%'";
    }

}
return $sql_where;

}
//select * from products where 1 OR color LIKE '%GOLD%' OR color LIKE '%RED%' 
备注:

输入:GOLDRED

匹配:金红色、金红色、金/红色。。。。。金/红/ABC,红/金绿


可能是在获得所有数据后,然后按匹配%对func进行排名,比如搜索引擎不是最好的方法,而且我肯定有很多缺点,但是如果我在php代码中没有犯任何错误(没有机器来尝试),它将完成以下工作:

"select * from products where color REGEXP '".cleanstring($stringval)."' limit 1";

function cleanstring($var) {
   $var = preg_replace('![-\/ ]+!', '', $var);
   $strLength = strlen($var);
   $parts = array();
   for ($i = 1; $i <= $strLength; i++) {
     $parts[] = ($i > 0) ? substr($var, 0, $i).'[-/ ]?'.substr($var, $i);
   }
   return "[[:<:]](".implode('|', $parts).")[[:>:]]";    
}
“从颜色REGEXP'.cleanstring($stringval)。“‘限制1’”的产品中选择*;
函数cleanstring($var){
$var=preg\u replace(“![-\/]+!”,“$var);
$strLength=strleng($var);
$parts=array();
对于($i=1;$i0)?substr($var,0,$i)。“[-/]?”。substr($var,$i);
}
返回“[[::]]”;
}
它将输出如下内容:

"select * from products where color REGEXP '[[:<:]](G[-/ ]?OLDRED|GO[-/ ]?LDRED|GOL[-/ ]?DRED|GOLD[-/ ]?RED|GOLDR[-/ ]?ED|GOLDRE[-/ ]?D)[[:>:]]' limit 1"
“从颜色REGEXP'[[::]]限制为1的产品中选择*”
这基本上把你的关键词一个字母一个字母地分成了几部分,即

  • 奥德雷德
  • 发红
  • 戈尔德雷德
  • 金红
  • 戈尔德埃德
  • 戈德雷D

然后对它们执行“LIKE”语句,但使用更智能的单词边界,而不仅仅是空格,它还考虑“
-
”和“
/
”。

也许您可以使用“GOLD.?RED”或“GOLD”([space:])?RED来生成一个MySQL regexp


这是我在网上做的一个例子:

你试过像“%GOLDRED%”这样的
子句
我试过了,问题是它不能选择那些带有hypens、斜杠和空格的。不太熟悉mysql中的
soundex()
函数,但你试过了吗?没有试过soundex(),我检查过soundex,它看起来很有趣,但有点不同。你必须在颜色列表中以金红色或其他文字获得断点,请参阅我的答案。我将检查这个Joe Lee。Joe Lee,有个问题,你查过密码了吗。$sql_中有错误,其中“和类似“{$v}%”的颜色”;JOELEE,谢谢你的密码,部分正确。主要问题在于内容。颜色范围要比这复杂得多。有些甚至没有颜色名称。由于颜色和其他值的范围,我不可能提供$color_列表数组所需的所有内容。