drhzn3911 2019-01-02 09:58
浏览 26

如何为任何字符和空格制作正则表达式

I'm making a search form, so I want the user to enter any word or string which contains alpha numeric and space between the words.

I want to allow alpha numeric and spaces in the search bar but so far I'm not successful. I did research on google to find something that can help me to do this but so far no luck. I need your help on how to write the regular expression for preg_match So what i am trying to do here is replace any character that is not 0-9 a-z A-Z and space be replaced with ""

$searchq = mysql_prep(urldecode($_GET['q']));
$searchq = preg_replace("#[^0-9a-z\s+]#i", "", $searchq);

EDIT: After getting deep into what is happening and what i want to achieve, i found out that the problem is with my query and not my regex ! If the user enters for example first name and last name eg John Doe, i want the search result to get back with John doe from database ! so I need please your help with my query on how can i make this achievable, i have 2 columns one for First name which is "first_name" and one for last name which is "last_name"

here is my query :

$query_search2 = "SELECT * FROM users_table WHERE first_name LIKE '%$searchq%' OR last_name LIKE '%$searchq%' OR username LIKE '%$searchq%' OR first_name LIKE '%$searchq%' AND last_name LIKE '%$searchq%'";
  • 写回答

3条回答 默认 最新

  • doutaoer3148 2019-01-02 14:28
    关注

    You can use the following Regex, which will find All alphabets regardless of case. A-Z, a-z, 0-9, white spaces which includes tabs, and underscore.

    Regex: '(?:\w+|\s+){1,}'

    If you do not want to capture Underscore, you can use the following:

    Regex: '([A-Za-z0-9]+|\s+){1,}'

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?