Jump to content

[SOLVED] Grabbing a section of a URL


Canman2005

Recommended Posts

Hi all

 

I have a URL which looks like

 

http://www.domain.com/165-products-electronic-music.html

 

Is it possible to grab a section of that URL and set it as a variable

 

the part I want to grab is the number, so with the example above it woud be

 

165

 

or of the URL was

 

http://www.domain.com/765-apple-desktop.html

 

it would grab

 

765

 

Any help would be great

 

thanks in advance

 

ed

Link to comment
Share on other sites

you can grab variables from URLs using the $_get[] method you can send variables from one page to another

 

e.g you searched for a user then when u click the usersname in sends you to profile.php what you need to do is send ?profile=Danny to profile.php then Danny would be the varable

 

Link to comment
Share on other sites

Hi,

Firstly either your server needs to be configured to parse html pages as php (can be done via htaccess) or you need to change your extensions to .php.

 

Secondly, assuming that the numeric value is always in the same place (ie first in your exampes) and that they are sperated by a hyphen you could do this:

//  get the file name from the url (note - there are several ways to get this)
$file=basename($_SERVER["PHP_SELF"]);

// split $file up using the hyphen - creates an array
$file_parts=explode("-",$file);

//define the first item in the array as your variable (maybe add controls to check it is numeric)
$your_var=$file_parts[0];

 

Hope that helps :)

 

Chris

Link to comment
Share on other sites

$_SERVER["PHP_SELF"] might return a slash or two, so it might be useful to use trim as well.

 

<?php
$file= $_SERVER["PHP_SELF"];

// split $file up using the hyphen - creates an array
$file_parts = explode("-", trim($file, "/"));

//define the first item in the array as your variable (maybe add controls to check it is numeric)
$your_var = $file_parts[0];

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.