Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is preg_match() with pattern "75.1." and subject "75.142.3.8" returning a match in PHP?

I am testing the matching of a function I am using to check IP ranges.

$a = "75.1.";
$b = "75.142.3.8";
$num = preg_match("/^$a/", $b, $matches);
echo "$a<br/>\n$b<br/>\n$num";

This is returning a $num of 1 for me. Why is this returning a match, despite "1." not matching "142"? Am I making some error with regular expressions? strstr($b, $a) and str_starts_with($b, $a) are both returning FALSE, as I was expecting.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

. is a wild character, you’ll need to escape it with a \

$a = "75\.1\.";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading