natasha_thomas Posted May 12, 2011 Share Posted May 12, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/236174-extractign-a-substring/ Share on other sites More sharing options...
gizmola Posted May 12, 2011 Share Posted May 12, 2011 Is it always the number here? d/....../ Quote Link to comment https://forums.phpfreaks.com/topic/236174-extractign-a-substring/#findComment-1214258 Share on other sites More sharing options...
kenrbnsn Posted May 12, 2011 Share Posted May 12, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/236174-extractign-a-substring/#findComment-1214259 Share on other sites More sharing options...
natasha_thomas Posted May 12, 2011 Author Share Posted May 12, 2011 Is it always the number here? d/....../ Thanks for the Reply Gizmola. R u asking the str i want to extract will it be Number? - It will always be Alphanumeric. Quote Link to comment https://forums.phpfreaks.com/topic/236174-extractign-a-substring/#findComment-1214263 Share on other sites More sharing options...
natasha_thomas Posted May 12, 2011 Author Share Posted May 12, 2011 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). Quote Link to comment https://forums.phpfreaks.com/topic/236174-extractign-a-substring/#findComment-1214265 Share on other sites More sharing options...
gizmola Posted May 12, 2011 Share Posted May 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236174-extractign-a-substring/#findComment-1214266 Share on other sites More sharing options...
natasha_thomas Posted May 12, 2011 Author Share Posted May 12, 2011 One last Request: http://my.domain.com/Pages/ViewItem.aspx?aid=230620298886&sv=paintball&emvcc=0 Need to extract: 230620298886 Cheers N Quote Link to comment https://forums.phpfreaks.com/topic/236174-extractign-a-substring/#findComment-1214267 Share on other sites More sharing options...
kenrbnsn Posted May 12, 2011 Share Posted May 12, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/236174-extractign-a-substring/#findComment-1214270 Share on other sites More sharing options...
natasha_thomas Posted May 12, 2011 Author Share Posted May 12, 2011 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.... Quote Link to comment https://forums.phpfreaks.com/topic/236174-extractign-a-substring/#findComment-1214271 Share on other sites More sharing options...
gizmola Posted May 12, 2011 Share Posted May 12, 2011 Haha, I just sat back while Ken did all the work... sorta like management. Quote Link to comment https://forums.phpfreaks.com/topic/236174-extractign-a-substring/#findComment-1214272 Share on other sites More sharing options...
kenrbnsn Posted May 12, 2011 Share Posted May 12, 2011 Haha, I just sat back while Ken did all the work... sorta like management. That's ok -- I like hands-on better than management Ken Quote Link to comment https://forums.phpfreaks.com/topic/236174-extractign-a-substring/#findComment-1214274 Share on other sites More sharing options...
natasha_thomas Posted May 12, 2011 Author Share Posted May 12, 2011 Thanks the Beauty of being here... Many seniors are all here to Guide.... Cheers Quote Link to comment https://forums.phpfreaks.com/topic/236174-extractign-a-substring/#findComment-1214277 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.