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.';
  }
}

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.