PHP regex to find any number at the beginning of a line

I am using PHP and I hope that I can create a regular expression that will find and return the street number of the address.

Example:

1234- South Blvd. Washington DC, APT # 306, ZIP45234

In the above example, only 1234 will be returned.

It sounds like it should be incredibly simple, but I haven't got it yet. Any help would be greatly appreciated.

+3
source share
2 answers

Try the following:

$str = "1234- South Blvd. Washington D.C., APT #306, ZIP4523";
preg_match("~^(\d+)~", $str, $m);
var_dump($m[1]);

CONCLUSION:

string(4) "1234"
+4
source

This will probably happen in many cases - the address line does not always start with a line of numbers, etc.

See this question .

+1
source

All Articles