Page 1 of 1

using explode ?

Posted: Wed Jun 04, 2008 3:35 pm
by pedroz
I have the following string
$CLC_string = "london,en-gb,united kingdom;manchester,en-gb,united kingdom;madrid,es,spain";

if ($city="london") Then {$language="en-gb"; $country="united kingdom";}
if ($city="manchester") Then {$language="en-gb"; $country="united kingdom";}
if ($city="madrid") Then {$language="es"; $country="spain";}

What is the fastest way to get the $language and $country strings from $CLC_string?

Thanks ;)

Re: using explode ?

Posted: Wed Jun 04, 2008 3:37 pm
by Frozenlight777
Maybe try using an array instead of a long string.

Re: using explode ?

Posted: Wed Jun 04, 2008 3:43 pm
by califdon
pedroz wrote:I have the following string
$CLC_string = "london,en-gb,united kingdom;manchester,en-gb,united kingdom;madrid,es,spain";

if ($city="london") Then {$language="en-gb"; $country="united kingdom";}
if ($city="manchester") Then {$language="en-gb"; $country="united kingdom";}
if ($city="madrid") Then {$language="es"; $country="spain";}

What is the fastest way to get the $language and $country strings from $CLC_string?

Thanks ;)
Use explode() with the ";" separator to form an array like:

Code: Select all

london,en-gb,united kingdom
manchester,en-gb,united kingdom
madrid,es,spain
Then you can search the array: http://www.phpf1.com/tutorial/php-array-search.html

Re: using explode ?

Posted: Thu Jun 05, 2008 12:36 pm
by [UW] Jake

Code: Select all

$CLC_string = "london,en-gb,united kingdom;manchester,en-gb,united kingdom;madrid,es,spain";
If you want to key by city, would recommend using something like:

Code: Select all

 
$CLC = array();
$CLC['London'] = array('language' => 'en-gb', 'country' => 'United Kingdom');
$CLC['Manchester'] = array('language' => 'en-gb', 'country' => 'United Kingdom');
$CLC['Madrid'] = array('language' => 'es', 'country' => 'Spain');
 
/*
 
CODE TO OBTAIN THE $city VARIABLE
OR SOMETHING
 
*/
 
//We now have the city variable
if(CLC[$city] != NULL)
{
  echo 'Your language will be ' . $CLC[$city]['language'] . '<br />' . "\n";
  echo 'Your country will be ' . $CLC[$city]['country'] . '<br />' . "\n";
}
else
{
  echo "We don't have an entry for the city "" . $city . "".<br />\n";
}
 
 
 
 
 
 
 

Another way you could do it with 1 explode is:

Code: Select all

 
//notice the semicolons are commas
$CLC_string = "london,en-gb,united kingdom,manchester,en-gb,united kingdom,madrid,es,spain";
$info = explode(',', $CLC_string);
for($i = 0; $i < count($info) - 2; $i += 3)
{
  $city = $info[$i+0]; //don't necessarily need + 0 but it's a visual thing
  $language = $info[$i+1];
  $country = $info[$i+3];
 
  //Do something with the variables
}
 
//All done
 

And the last way, using the same structure as your string

Code: Select all

 
$CLC_string = "london,en-gb,united kingdom;manchester,en-gb,united kingdom;madrid,es,spain";
$entries = explode(';', $CLC_string);
for($i = 0; $i < count($entries); $i++)
{
  $info = explode(',', $entries[$i]);
  $city = $info[0];
  $language = $info[1];
  $country = $info[2];
 
  //Looks like you want to key off city so
  if($city == $match_city)
    break;
}
 

Re: using explode ?

Posted: Fri Jun 06, 2008 1:04 am
by RobertGonzalez
Why not use a regular expression?