php_begins Posted November 23, 2011 Share Posted November 23, 2011 Hi i need help with some string manipulation and extraction.. http://www.test.com/forums/attachment/f39/745d1267084199-official-ar-m16-m4-picture-thread-dsc00005.jpg I need to extract an id from the above path dynamically. I need to extract the digits before the alphabet d after the second last path f39/ In the above case the number extracted would be 745. Can someone please help me out with it? Quote Link to comment https://forums.phpfreaks.com/topic/251695-extract-part-of-a-string/ Share on other sites More sharing options...
requinix Posted November 23, 2011 Share Posted November 23, 2011 You'd probably want regular expressions for this. What are you scraping from, and into where? Quote Link to comment https://forums.phpfreaks.com/topic/251695-extract-part-of-a-string/#findComment-1290795 Share on other sites More sharing options...
joe92 Posted November 23, 2011 Share Posted November 23, 2011 preg_match("/(?<=f39\/)([\d]+)/", $thatURL, $match); That will match all the digits after 'f39/' until it stops reading digits, and will then place that match in an array called $match. Hope this helps, Joe Quote Link to comment https://forums.phpfreaks.com/topic/251695-extract-part-of-a-string/#findComment-1290796 Share on other sites More sharing options...
php_begins Posted November 23, 2011 Author Share Posted November 23, 2011 Thanks but the thing is f39 is not constant..it may vary for different websites..i need to extract the number after the last slash / Can this be done? Quote Link to comment https://forums.phpfreaks.com/topic/251695-extract-part-of-a-string/#findComment-1290798 Share on other sites More sharing options...
PaulRyan Posted November 23, 2011 Share Posted November 23, 2011 <?PHP //### Only get what is needed of the actualy URL $string = 'http://www.test.com/forums/attachment/f39/745d1267084199-official-ar-m16-m4-picture-thread-dsc00005.jpg'; $string = end(explode('/',$string)); //### Preg_match the "d" to return numbers only preg_match("/[\d]{1,}/", $string,$match); print_r($match); ?> Quote Link to comment https://forums.phpfreaks.com/topic/251695-extract-part-of-a-string/#findComment-1290800 Share on other sites More sharing options...
joe92 Posted November 23, 2011 Share Posted November 23, 2011 You could use pathinfo to get the basename and run the preg_match on that: $url = "http://www.test.com/forums/attachment/f39/745d1267084199-official-ar-m16-m4-picture-thread-dsc00005.jpg"; $getpath = pathinfo($url); $basename = $getpath[basename]; $getpath = preg_match("/^[\d]+/", $basename, $match); Quote Link to comment https://forums.phpfreaks.com/topic/251695-extract-part-of-a-string/#findComment-1290826 Share on other sites More sharing options...
php_begins Posted November 23, 2011 Author Share Posted November 23, 2011 thanks a lot...another useful solution i found was this: $str = 'http://www.test.com/forums/attachment/f39/6066d1267084199-official-ar-m16-m4-picture-thread-dsc00005.jpg'; $filename = pathinfo($str, PATHINFO_FILENAME); $part = substr($filename, 0, strpos($filename, 'd') - strlen($filename)); print $part; Quote Link to comment https://forums.phpfreaks.com/topic/251695-extract-part-of-a-string/#findComment-1290834 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.