Reputation: 127
I have a string with text and french zip code and french city.
$str="lore ipsum facto lore ipsum 75000 Paris";
I would like to extract : "75000 Paris"
I know how to extract the zip code :
preg_match('/(\d{5})/',$str, $matches);
But I don't know how to add also the city after. An idea to help me ?
Thank you !
Upvotes: 1
Views: 66
Reputation: 3002
You can update your regex to:
preg_match('/(\d{5} [a-zàâçéèêëîïôûùüÿñæœ\- ]+)/i',$str, $matches);
This way you check after the zipcode to have a space and letters (a-z or special french characters (lower case or upper case)), -
or an empty space. I think this cover all cities.
For example for this string:
lore 99912345 La Roche-sur-Yon
the match will be: 12345 La Roche-sur-Yon
Avenue de Baixas 66240 Saint-Estève
will match: 66240 Saint-Estève
Upvotes: 3
Reputation: 1791
Another way to do this
preg_match('/(\d{5}\s\S+)/',$str, $matches);
You will find your result in $matches array.
Note : this will only work with city names with one word. If you need multiple word support then may be regrex isn't the perfect thing because it may fetch unnecessary word too.
Upvotes: 0