Php 如何添加Preg Match以匹配带点、撇号的字符串
如何使Preg匹配到使用Php 如何添加Preg Match以匹配带点、撇号的字符串,php,regex,preg-match,Php,Regex,Preg Match,如何使Preg匹配到使用、'、“和搜索字符串?我尝试使用以下代码,但如果字符串包含,`,则该代码无效 (preg_match(“/\b”.preg_quote($txtsize,“/”)。“\b/i,$row['itemsdescription')) 使用上述代码,我无法在搜索时获得此信息31X15.50-15 12层TRAXION HF2或31X15.50或1.75“X11.500”或 谢谢您可以使用这样的正则表达式: ^[a-z.'"#]+$ ^--- Put all the c
、'
、“
和
搜索字符串?我尝试使用以下代码,但如果字符串包含
,`,则该代码无效
(preg_match(“/\b”.preg_quote($txtsize,“/”)。“\b/i,$row['itemsdescription'))
使用上述代码,我无法在搜索时获得此信息31X15.50-15 12层TRAXION HF2
或31X15.50
或1.75“X11.500”
或
谢谢您可以使用这样的正则表达式:
^[a-z.'"#]+$
^--- Put all the characters you consider valid within the character class
define as [...]
从图形上看更容易理解
您可以使用此代码
$re = "/^[a-z.'\"#]+$/mi"; // Notice insensitive flag: i
$str = "YOUR STRING HERE";
preg_match($re, $str, $matches);
但是,如果要传递这些字符串:
31X15.50-15 12 PLY TRAXION HF2
31X15.50
您必须在正则表达式中允许空格、连字符和数字,因此您必须使用:
^[a-z.'"# 0-9-]+$
↑
| below the code
|
$re = "/^[a-z.'\"#]+$/mi";
什么是
$txtsize
?最好显示一些示例输入和预期输入matches@anubhava包含文本框中的值$txtsize=$\u请求['search\u by\u size']
;Is31X15.50-15 12层TRAXION HF2
$row['itemsdescription']
或$txtsize
?请发布其他变量值。@chris85即$row['itemsdescription']
好的,比$txtsize
更重要的是什么?不是$\u请求['search\u by\u size']
,而是实际值。非常感谢@Fede我将我的代码更新为$re=“/^[a-z.\'”\\\\+$/mi”$str$\u请求['search_by_size'];和Preg Match看起来像elseif($str!=“”)和&Preg_Match($re,$str,$row['itemsdescription']))| |($row['itemsdescription']=$str)){…显示结果…}。但我不知道我做错了什么?@Aaron我不知道变量中有什么值,我想你应该用调试器来解决它