Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Regex to Find Any Number at the Beginning of String

Tags:

regex

php

I'm using PHP, and am hoping to be able to create a regex that finds and returns the street number portion of an address.

Example:

1234- South Blvd. Washington D.C., APT #306, ZIP45234

In the above example, only 1234 would be returned.

Seems like this should be incredibly simple, but I've yet to be successful. Any help would be greatly appreciated.

like image 300
Brian Avatar asked May 05 '11 04:05

Brian


1 Answers

Try this:

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

OUTPUT:

string(4) "1234"
like image 180
anubhava Avatar answered Nov 07 '22 02:11

anubhava