Solved

Extract 2 parts of URL

Posted on 2008-10-27
2
213 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn how to count occurrences of each item in an array.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now