Solved

Extract 2 parts of URL

Posted on 2008-10-27
2
212 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
Comment Utility
if(preg_match(/http\:\/\/.*\/([0-9]+)_(\w+)-.*/, $url, $matches)){
echo matches[1];
echo matches[2];
}
0
 
LVL 6

Expert Comment

by:hampus_b
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

771 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

10 Experts available now in Live!

Get 1:1 Help Now