Jump to content

How to Sanitize a URL - How do I get a piece of a URL into a variable?


BrazilMac

Recommended Posts

Hello!

 

Im failry new to PHP and I guess this is a complex thing that I no idea how to do.

 

I have an Ebay URL for an auction, they all look like this:

 

http://cgi.ebay.com/Heavy-Duty-Cuckoo-Clock-Bellow-Part-Wires-5-1-2_W0QQitemZ270262100267QQihZ017QQcategoryZ10020QQssPageNameZWDVWQQrdZ1QQcmdZViewItem

 

It seems all normal auctions have the item number inbetween the "itemZ" and "QQih..." section.

 

Basically, I just need to copy the section in red above (this is the item number)into a variable in PHP.

 

How do I go about reading the URL and just extracting it??

 

Thanks again!!

 

Herick

Use of regex will do it:

 

<?php
$url = 'http://cgi.ebay.com/Heavy-Duty-Cuckoo-Clock-Bellow-Part-Wires-5-1-2_W0QQitemZ270262100267QQihZ017QQcategoryZ10020QQssPageNameZWDVWQQrdZ1QQcmdZViewItem';
$chars = preg_split('/http.*QQitemZ/', $url, -1, PREG_SPLIT_OFFSET_CAPTURE);
$chars = preg_split('/QQih.*ViewItem/', $chars[1][0], -1, PREG_SPLIT_OFFSET_CAPTURE);
$chars = $chars[0][0];
echo $chars;
?>

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.