1

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 !

4
  • /(\d{5}).+/ should do it, infact you don't need the brackets: \d{5}.+\ Commented Oct 19, 2016 at 14:02
  • 1
    Can there be text after Paris? Commented Oct 19, 2016 at 14:03
  • 1
    What about cities with multiple "words"? What about strings that just happen to have a 5 digit number? Have these been considered? Commented Oct 19, 2016 at 14:05
  • My solution will work for multiple words in the city name assuming that the city name is definitely the last thing that occurs in the string, otherwise this becomes much more of a problem and I don't think regex can be of much help. Commented Oct 19, 2016 at 14:06

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

6 Comments

This is a better answer than mine in the comments because it will only get the next word after the postcode and accounts for the space between the two. Only thing that could cause you problems is if there are 5 numerical characters in the string before the postcode. Although, this will only work for city names with one word in their name.
If you have some numeric before the zipcode it will get only the last 5 and the string after that. For example for this input lore 99975000 Paris City will return: 75000 Paris City
Doesn't works for "66100 Saint-Estève" its stop on Saint-Est
@Bisvan I updated my answer with special french characters. I Think I added all of them. Also I added /i so it will be case insensitive (accepts lower case and upper case)
$test="Avenue de Baixas 66240 Saint-Estève"; preg_match('/(\d{5} [A-Zzàâçéèêëîïôûùüÿñæœ- ]+)/i',$test, $matches); print_r($matches);
|
0

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.

1 Comment

There are city names which have spaces in them. Los Angeles being one of the more famous ones, and I'm positive far from the only one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.