-1

Quick question to easily cut a string. I know the substr but I can not use it in my case.

I have for example this thong: "1 rue Maryse Bastie 69500 BRON" I'd like to say "Get all the end before the numbers" in this case "BRON" and get the numbers. I want the city and the postalcode in difference variable.

The API I'm using returns the complete address but I'd like to cut out to save the city and postal code in a different table.

I think this subject can help me : Extracting a zip code from an address string but not to get the city.

For exemple, if i have 1 rue Maryse Bastie 69500 BRON I want $city = "BRON" and $pc = "69500"

If i have 13 rue Hohwald 67000 Strasbourg I want $city = "Strasbourg" and $pc = "67000"

Thanks

Jan Myszkier
  • 2,714
  • 1
  • 16
  • 23
TiboK
  • 95
  • 3
  • 12

2 Answers2

1

I give you a workaround, it should totally works, but maybe you will have to improve it :

$test = '1 rue Maryse Bastie 69500 BRON';
\preg_match('/\b\d{4,5}.+$/i', $test, $out);
$output = \explode(' ', \trim($out[0]), 2);
var_dump($output);
/*
array(2) {
  [0]=>
  string(5) "69500"
  [1]=>
  string(4) "BRON"
}
*/

In the code above we have used regex to find this way :

  • find digits (many characters --> 4 or 5 five for french zipcode)
  • find all characters next the zipcode until the end

Next we remove spaces at start and end, then we split into an array.

Lounis
  • 597
  • 7
  • 15
  • Its work fine, just a small problem. For exemple, a city was call "St Quentin", the code display only "St" – TiboK Feb 16 '20 at 19:16
  • In this case you can limit the explode with an third argument like this : `$output = \explode(' ', \trim($out[0]), 2);` It will split string into an array of two items, so only split at first space encountered. I edit my answer for your case. – Lounis Feb 16 '20 at 19:30
  • The problem its how to know if they have 2 argument or 1 ? – TiboK Feb 16 '20 at 21:01
  • This will be everytime limited to `2`, `$output = \explode(' ', \trim($out[0]), 2);` will be correct in all cases. I tested in all cases like `1 rue Maryse Bastie 69500 ST BRON EN YVELINES, 13 rue Hohwald 67000 Strasbourg`. – Lounis Feb 16 '20 at 21:28
  • It's work ! Thanks man. Thanks a lot for your help ! :) Good evening – TiboK Feb 19 '20 at 20:13
  • The case-insensitive flag serves no purpose. – mickmackusa Aug 21 '22 at 02:17
1

If you are getting your string in the same format as you mentioned in your question, you can break the string into an array and collect the last two elements as you required values.

Let me show you.

$myString = "1 rue Maryse Bastie 69500 BRON";
$breakUP = explode(" ", $myString);
$totalElement = count($breakUP);
$city = $breakUP[$totalElement - 1]; // Last element
$zip = $breakUP[$totalElement - 2]; // Second Last element

Hope this helps you :)

mail2bapi
  • 1,547
  • 1
  • 12
  • 18
  • Thanks for your help mail2bapi. Just, for example i have one city call "St Quentin". So your script return $city = Quentin, $zip = St – TiboK Feb 16 '20 at 19:18