【问题标题】:PHP submitting wildcard characters in a stringPHP在字符串中提交通配符
【发布时间】:2014-03-05 09:32:22
【问题描述】:

我有一个将代码插入数据库的系统,例如 AREF0OGS 就是一个代码。

我遇到的问题是,当代码分发时,0 和 O 之间的差异难以辨认,所以我需要一种方法,当前系统可以将 0 和 O 同等对待(只要它在同一个字符串的一部分)。

这是我所拥有的:

$code = mysql_real_escape_string($_POST['code']);  
$userid = mysql_real_escape_string($_POST['userid']); 

$resultcode = mysql_query('select * from xxxxxx where code = "'. $code .'"' ) or die(mysql_error());  

有没有办法可以将 0 和 O 视为相同?在 PHP 或 MySQL 中?

谢谢

【问题讨论】:

  • 'select * from xxxxxx where code LIKE "'. str_replace(array('0','O'), '_', $code) .'"'
  • 不要使用 Mysql_* 朋友,因为它们现在已经过时了。请改用 PDO 或 Mysqli。上面的解决方案应该适合你:)
  • @MarkBaker 不匹配 AREFAAGS 和 AREFIOGS 和 AREF(any-two-letters)GS 吗?我不认为这是他们想要的结果。

标签: php mysql string wildcard


【解决方案1】:

我认为您最好的选择是在您进行查询之前,您可以将其中一个替换为另一个。这可以在任一 MySQL 中完成

select * from table where code=replace($code, 'O', '0')

或者在 PHP 中

$code = str_replace('O', '0', $code);

【讨论】:

    【解决方案2】:

    只需始终将 O 字母替换为 0 数字(或其他方式)。

    $code = mysql_real_escape_string($_POST['code']);   $code =
    str_replace('0', '0', $code); $userid =
    mysql_real_escape_string($_POST['userid']); 
    
    $resultcode = mysql_query('select * from xxxxxx where code = "'. $code .'"' ) or die(mysql_error());
    

    【讨论】:

      猜你喜欢
      • 2014-06-14
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多