using explode ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

using explode ?

Post 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 ;)
Last edited by pedroz on Wed Jun 04, 2008 3:39 pm, edited 2 times in total.
User avatar
Frozenlight777
Forum Commoner
Posts: 75
Joined: Wed May 28, 2008 12:59 pm

Re: using explode ?

Post by Frozenlight777 »

Maybe try using an array instead of a long string.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: using explode ?

Post 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
User avatar
[UW] Jake
Forum Commoner
Posts: 25
Joined: Sun Jun 01, 2008 9:04 pm
Location: USA

Re: using explode ?

Post 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;
}
 
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: using explode ?

Post by RobertGonzalez »

Why not use a regular expression?
Post Reply