How can add Preg Match to Match String with dot, Apostrophes

504 views Asked by At

How can I make Preg Match to Search Strings With ., '," and #? I tried used the following code but it doesnt work if the string contains ., `' .

(preg_match("/\b".preg_quote($txtsize,'/')."\b/i", $row['ItemDescription'])

Using the above code i couldnt get this on Search 31X15.50-15 12 PLY TRAXION HF2 or 31X15.50 or 1.75"X11.500" or

Thank you

1

There are 1 answers

2
Federico Piazza On BEST ANSWER

You can use a regex like this:

^[a-z.'"#]+$
      ^--- Put all the characters you consider valid within the character class
           define as [...]

Graphically is more understandable...

Regular expression visualization

Working demo

You can use this code

$re = "/^[a-z.'\"#]+$/mi"; // Notice insensitive flag: i
$str = "YOUR STRING HERE"; 

preg_match($re, $str, $matches);

However, if you want to pass these strings:

31X15.50-15 12 PLY TRAXION HF2 
31X15.50

You'll have to allow spaces, hyphens and numbers in your regex, so you'll have to use:

^[a-z.'"# 0-9-]+$
      ↑
      | below the code
      |
$re = "/^[a-z.'\"#]+$/mi";