Link to home
Create AccountLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

I gather values from URL

Hello,

My URL looks like this:

      $myURL="http://example.com/book/shop/language_en/locale_ca/mode_books/page_3/search_food/";

I want the following variables to be gathered from the URL:

      $language = 'en';
      $locale='ca';
      $mode='books';
      $page='3'
      $search='food';

How can I gather the values from the URL?
Avatar of rowejd
rowejd
Flag of United States of America image

Be careful with the input--you might want to add some functions to protect the incoming variables--but attached is one way to accomplish what you want:
$example='http://example.com/book/shop/language_en/locale_ca/mode_books/page_3/search_food/';
$separated = explode('/', $example);
$language = explode('_', $separated[5]);
$locale = explode('_', $separated[6]);
$mode = explode('_', $separated[7]);
$page = explode('_', $separated[8]);
$search = explode('_', $separated[9]);
$language = $language[1];
$locale = $locale[1];
$mode = $mode[1];
$page = $page[1];
$search = $search[1];

Open in new window

Avatar of hankknight

ASKER

Thanks for the idea.

I was not clear enough in my description.

The order of the values may change.
  $myURL="http://example.com/book/shop/language_en/locale_ca/mode_books/page_3/search_food/";
  $myURL="http://example.com/book/shop/page_3/search_food/locale_ca/mode_books/language_en/";
SOLUTION
Avatar of rowejd
rowejd
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
The function I gave already will do the trick - just copy the if statement 3 times and change the string and it's "done".
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account