Link to home
Start Free TrialLog in
Avatar of interclubs
interclubs

asked on

Extract 2 parts of URL

I'm working in PHP. I am trying to extract the '381120275' and the 'HWioi' from the below url. THe length of these items might change, but they will always be in the below format....

http://someplace.somewhere.com/photos/381120275_HWioi-Th.jpg
like below:
xxxxxxxxxx/NUMBER1_ID2-xxxxxxxxxxxx.XXX
ASKER CERTIFIED SOLUTION
Avatar of sh0e
sh0e

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
see code snippet below. I'm not really sure what parts are fixed... the xxxxx in your example, can they all change? ie different domains etc.

if not - this will work. if yes, please be more specific about which parts are fixed and which are not

//H
<?
$string = "http://someplace.somewhere.com/photos/381120275_HWioi-Th.jpg";
preg_match("|.*/photos/(\d*)_(.*)-Th.jpg|",$string,$matches);
 
echo $matches[1]; // outputs 381120275 
echo $matches[2]; // outputs HWioi 
 
?>

Open in new window