0

Using PHP how can I split the

Szombathely, Hungary

into two variables

Szombathely
Hungary

Thank you!

EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179
  • 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,538
  • 4
  • 29
  • 47
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,973
  • 28
  • 27
0
$string = "Szombathely, Hungary";
$string = explode(", ", $string);
list($Szombathely, $Hungary) = $string;

var_dump($Szombathely); //Szombathely
var_dump($Hungary);//Hungary` 
user990717
  • 470
  • 9
  • 18