Jump to content

Getting a number from an address


Chicken

Recommended Posts

one thing you could do is explode the URL using forward slashes into a series of array items, or use parse_url().  note that parse_url() may fail with deformed URLs, so it may be less reliable.

 

when exploding, you could simply search until you find something numeric and 7 characters long.  if you KNOW that the 7-digit number will reside in the same spot in the path everytime, you don't even need to search:

 

$url = 'http://site.com/something/Variable_length/numberhere/varialble_length';
$pieces = explode('/', $url);

// if you KNOW the format of the URL will always be the same:
echo 'number is: '.$pieces[4];

// if you don't necessarily know that the number will be there:
foreach ($pieces AS $piece)
{
  if (is_numeric($piece) && strlen($piece) == 7)
  {
    echo 'the number is '.$piece.', i THINK.';
  }
}

Link to comment
Share on other sites

I've got another problem about the same thing except this time i need to get an alphanumeric string of from the end of a php

example:

htp://www.place.com/blah?v=sRv953XZX6Y

 

the length of the alphanumeric string is variable but the "htp://www.place.com/blah?v=" remains the same length

Link to comment
Share on other sites

then you can use parse_url(), grabbing the 'query' portion:

 

$url = 'http://www.place.com/blah?v=sRv953XZX6Y';
$components = parse_url($url);
$query = explode('=', $components['query']);
echo 'value of '.$query[0].' is '.$query[1];

 

this is far from ideal, but a possible solution.

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.