doushi1960 2016-09-13 21:56
浏览 27

Noob在这里:我无法理解这个正则表达式搜索背后的逻辑[重复]

This question already has an answer here:

I'm having trouble understanding the logic in the regular expression inside the preg_replace function. Specifically, I'm having trouble understanding the use of the caret inside the brackets. If I type a single letter/number that matches any letter/number in the username column, I'll get a match for that row. When I remove the caret, even when I don't search for anything, everything in my db matches. Can someone please elaborate on what's going on here? Thanks.

$searchq = $_POST['search'];
$searchq = preg_replace("/[^0-9a-z]/i","",$searchq );

$query = mysqli_query($conn, "SELECT * FROM people ".
"WHERE username LIKE '%$searchq%'") or die('Could not search.');
</div>
  • 写回答

2条回答 默认 最新

  • dounan9070 2016-09-13 21:59
    关注

    In basically every regex flavour, the square brackets [] denote a character class, which will match one instance of any character in it. A character class starting with a caret ^ is negated - i.e. it will match a single instance of any character that is not in it.

    In this case, your character class will match any character that isn't a number or lowercase letter. Because your regex is in ignore-case mode (the i modifier), it'll match any character that isn't a number or ASCII letter (either upper or lowercase), and the preg_replace will strip it out by replacing it with the empty string ("").

    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私