Solved

Extract 2 parts of URL

Posted on 2008-10-27
2
217 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:interclubs
2 Comments
 
LVL 16

Accepted Solution

by:
sh0e earned 500 total points
ID: 22810644
if(preg_match(/http\:\/\/.*\/([0-9]+)_(\w+)-.*/, $url, $matches)){
echo matches[1];
echo matches[2];
}
0
 
LVL 6

Expert Comment

by:hampus_b
ID: 22810658
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

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
php error 27 55
A Request to Review a (hopefully) simple Wordpress "if-or-else" php code line. 5 42
How to use session variables in php? 22 42
Echo'd values in dropdowns 6 27
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question