如何添加 Preg Match 以匹配带点、撇号的字符串
How can add Preg Match to Match String with dot, Apostrophes
如何使 Preg 匹配以使用 .
、'
、"
和 #
搜索字符串?我尝试使用以下代码,但如果字符串包含 .
, `' 则它不起作用。
(preg_match("/\b".preg_quote($txtsize,'/')."\b/i", $row['ItemDescription'])
使用上面的代码我无法在搜索 31X15.50-15 12 PLY 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";
如何使 Preg 匹配以使用 .
、'
、"
和 #
搜索字符串?我尝试使用以下代码,但如果字符串包含 .
, `' 则它不起作用。
(preg_match("/\b".preg_quote($txtsize,'/')."\b/i", $row['ItemDescription'])
使用上面的代码我无法在搜索 31X15.50-15 12 PLY 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";