Jump to content

Seperate number from end of url


frobak

Recommended Posts

Hi

 

I need to take the number off of the end of a url and do some processing with it.

 

so for example:

 

www.something.com/something/10001

 

i will need to seperate them into 2 different variables, for example:

 

$var1 = www.something.com/something/

$var2 = 10001

 

is this possible?

 

Thanks

Link to comment
Share on other sites

$var2 = substr(strrchr(__FILE__, '/'), 1);
$var1 = substr(__FILE__, 0, -(strlen($var2)));

 

I believe this will work, but it's untested, I'm just trying to work it out in my head so I strongly suggest testing it first and if it's not giving you what you expect, report back on your results.

Link to comment
Share on other sites

Actually, __FILE__ might not do what I'm thinking.

 

In any event, your 2nd problem is more straight forward. You can simply replace __FILE__ with wherever the user input is stored... IE. if it was submitted in a text box named "url" you'd replace __FILE__ with $_POST['file']

 

But you should make sure to validate user input in advance!

 

$var2 = substr(strrchr($_POST['url'], '/'), 1);
$var1 = substr($_POST['url'], 0, -(strlen($var2)));

Link to comment
Share on other sites

You might be able to make a more dynamic solution from this:

function segment($index = 1)
{
$segments = explode ('/', $_SERVER['REQUEST_URI']);

if (!empty($segments)) {
	array_shift($segments);

	return $segments[$index - 1];
}

return null;
}

 

To use, simply count the number of "segments" after your domain and there you go. So in "www.something.com/something/10001", doing echo segment(2) would echo "10001".

 

EDIT: By the way I didn't rigorously test this, just a push in the right direction.

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.