Page 1 of 1

Need to extract digits from URL in browser

Posted: Tue Jun 09, 2009 7:01 pm
by sig556
Hello Everyone

I am trying to extract 2-3 digits long from the browser url.

For example:

My url: http://www.xxx.com/category/15/Automobiles.html

I am trying to extract the value within the URL 15 and place it in a variable via php.

I am familiar with the following code however I don't know how to strip out the unneeded charters.

The site name and "category" will remain constant. The digits and text after that are variable.

Code: Select all

 
<?
$url = $_SERVER['SERVER_NAME'];
$page = $_SERVER['php_SELF'];
echo "http://".$url.$page;
?>
 
displays...

http://the_url.com/the_path_and_file_name


Could someone please point me in the right direction on how to do this?

Re: Need to extract digits from URL in browser

Posted: Tue Jun 09, 2009 8:09 pm
by omniuni
Hi sig556

Go here: http://us.php.net/manual/en/ini.core.ph ... er-globals

and scroll up a bit. Find the part about data handling and try arg_separator.input

Set it to use "/" as a separator. That should cause $_GET['category'] to be equal to the number that follows it.... well, for an index.php anyway.

Um, if you need to access it from a file that is in a sub directory, try using $_SERVER['php_self']; and taking a substring of two characters starting from position.... 9 (if I counted right) and that should result in "15".

Let me know if either of those help you out!

-OmniUni

Re: Need to extract digits from URL in browser

Posted: Mon Jun 15, 2009 12:28 pm
by PM2008
I assume you want to extract the section of the URL that contains all digits betwen /.../ . The number of digits is variable.

Here is a quick script. The URL is in variable $URL. Value of $URL begins with "http://" .


Code: Select all

while ($URL <> "")
do
    # Get the part till the next /.
    var str section ; stex "]^/^" $URL > $section
    if ( { sen -r "^(#0123456789)^" $section } <= 0 )
        # This is all digits.
        echo $section
    endif
    # Strip off the leading / for the next time.
    chex "1]" $URL > null
done

Script is in biterscripting ( http://www.biterscripting.com ) . Translate it appropriately to PHP.

Patrick

Re: Need to extract digits from URL in browser

Posted: Tue Jun 23, 2009 10:52 am
by CyG64
Hi,

with the substr(...) and the strpos(...) function you can do this.

First count the number of characters your domain/categories string is long.
Then with the strpos() find the next / and with the substr() extract a substring of a given length from the original string.

$url = "http://HoloGuides.com/abc/123/something.php";
$numberStart = strlen('http://HoloGuides.com/abc/');
$numberEnd = strpos($url, '/', $numberStart);
$number = substr($url, $numberStart, $numberEnd - $numberStart);
echo $number;


proud to be of service
http://HoloGuides.com/php