0

Using PHP how can I split the

Szombathely, Hungary

into two variables

Szombathely
Hungary

Thank you!

EnexoOnoma
  • 8,021
  • 16
  • 87
  • 172
  • And [How can I split a comma delimited string into an array in PHP?](http://stackoverflow.com/q/1125730) for the more contemporary `str_getcsv` and `preg_split`. – mario Nov 26 '14 at 20:11

3 Answers3

0

array explode ( string $delimiter , string $string [, int $limit ] )
Source: http://php.net/manual/en/function.explode.php

$split = explode(',','Szombathely, Hungary');

That will, however, leave you with space before _Hungary

So if all are ,_ you could use

$split = explode(', ','Szombathely, Hungary');

or use trim on each of array elements.

Grzegorz
  • 3,443
  • 4
  • 27
  • 43
0

There is a native command called explode, in which you pass as parameters the delimiter and also the string. More info here

In your case it would be:

$result = explode ( ',', 'Szombathely, Hungary')

as a result you get:

$result[0] = 'Szombathely'
$result[1] = 'Hungary'

Hope it helps!

facundofarias
  • 2,867
  • 25
  • 27
0
$string = "Szombathely, Hungary";
$string = explode(", ", $string);
list($Szombathely, $Hungary) = $string;

var_dump($Szombathely); //Szombathely
var_dump($Hungary);//Hungary` 
user990717
  • 470
  • 8
  • 17