Jump to content

Extractign a Substring...


natasha_thomas

Recommended Posts

Folks,

 

I want to extract something from a URL, i think we need to use regex for this.

 

Example URL:

http://www.amazon.co.uk/gp/aw/d/B004JN01LW/ref=mp_s_a_1?qid=1305169492&sr=8-1

 

What we need to Extract:

So we need to extract  B004JN01LW  from the URL.

 

I tried to use substr() but i think its not useful for this purpose, can someone help me with Regex to extract this?

 

Cheers

Natasha

 

Link to comment
Share on other sites

If the URL is always going to look similar to that example, you can do something like

<?php
$str = 'http://www.amazon.co.uk/gp/aw/d/B004JN01LW/ref=mp_s_a_1?qid=1305169492&sr=8-1';
$x  = parse_url($str);
list(,,,,$part) = explode('/',$x['path']);
echo $part . "<br>";
?>

 

Ken

 

Link to comment
Share on other sites

If the URL is always going to look similar to that example, you can do something like

<?php
$str = 'http://www.amazon.co.uk/gp/aw/d/B004JN01LW/ref=mp_s_a_1?qid=1305169492&sr=8-1';
$x  = parse_url($str);
list(,,,,$part) = explode('/',$x['path']);
echo $part . "<br>";
?>

 

Ken

 

Thanks Ken.. Works Good.

 

Thank you so much to both the Senior Members (Ken & Gizmola).

Link to comment
Share on other sites

no I was asking if the number always follows the /d/ in the url. 

 

Also check out Ken's code... a very high performance solution to it, not that it matters that much for one call whether it's optimal, but that is good code and uses some nice php functions there.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.