Jump to content

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
https://forums.phpfreaks.com/topic/236174-extractign-a-substring/
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

 

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).

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.

For that I would use parse_str as well as parse_url

<?php
$str = 'http://my.domain.com/Pages/ViewItem.aspx?aid=230620298886&sv=paintball&emvcc=0';
$x = parse_url($str);
parse_str($x['query'],$y);
echo $y['aid'] . "<br>";
?>

 

Ken

 

 

Again Thank You to Ken & Gizmola for Guidance... As always....

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.