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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.