Solved

Extract 2 parts of URL

Posted on 2008-10-27
2
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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