natasha_thomas Posted March 27, 2011 Share Posted March 27, 2011 Folks, http://natty.com/p/bh-fitness-class-indoor-magnetic-exercise-bike-2-years-parts-/detail/b004r2wuak/fitness-spinning.html From this url String, i want to extract the last part of string which is, "fitness spinning". This URL is dynamic an can have any value in that last bit, so how to extract anything btween Two Forward slashes just before .html? Note: It can not be extracted with GET as its not how its designed. Thanks Natasha Link to comment https://forums.phpfreaks.com/topic/231849-how-to-extract-certain-bit-from-string/ Share on other sites More sharing options...
yonny Posted March 27, 2011 Share Posted March 27, 2011 try this <?php $url="http://natty.com/p/bh-fitness-class-indoor-magnetic-exercise-bike-2-years-parts-/detail/b004r2wuak/fitness-spinning.html"; preg_match_all("/\/\w{2,20}-\w{2,20}.html/", $url, $matches); print"<pre>"; print_r($matches); print"</pre>"; $last = $matches[0][0]; $take_out = array('/', '-', '.html'); $put_in = array('', ' ', ''); $last = str_replace($take_out, $put_in, $last); print"$last"; ?> it outputs this Array ( [0] => Array ( [0] => /fitness-spinning.html ) ) fitness spinning Link to comment https://forums.phpfreaks.com/topic/231849-how-to-extract-certain-bit-from-string/#findComment-1192838 Share on other sites More sharing options...
QuickOldCar Posted March 27, 2011 Share Posted March 27, 2011 Hey nat. <?php function getLastPath($url) { $trim_path = trim($url, '/'); $parts = explode('/', $trim_path); $last = end($parts); return $last; } $insert_url = "http://natty.com/p/bh-fitness-class-indoor-magnetic-exercise-bike-2-years-parts-/detail/b004r2wuak/fitness-spinning.html"; echo getLastPath($_SERVER['REQUEST_URI']);//get current page echo '<br />'; echo getLastPath($insert_url);//get inserted url ?> Demo: http://get.blogdns.com/dynaindex/find-last-path.php Link to comment https://forums.phpfreaks.com/topic/231849-how-to-extract-certain-bit-from-string/#findComment-1192839 Share on other sites More sharing options...
natasha_thomas Posted March 27, 2011 Author Share Posted March 27, 2011 try this <?php $url="http://natty.com/p/bh-fitness-class-indoor-magnetic-exercise-bike-2-years-parts-/detail/b004r2wuak/fitness-spinning.html"; preg_match_all("/\/\w{2,20}-\w{2,20}.html/", $url, $matches); print"<pre>"; print_r($matches); print"</pre>"; $last = $matches[0][0]; $take_out = array('/', '-', '.html'); $put_in = array('', ' ', ''); $last = str_replace($take_out, $put_in, $last); print"$last"; ?> it outputs this Array ( [0] => Array ( [0] => /fitness-spinning.html ) ) fitness spinning Thansk but seems this code is not working for the below URL: http://natty.com/p/garmin-fr60-mens/detail/b004dnp2zs/fitness-watches-for-women.html Link to comment https://forums.phpfreaks.com/topic/231849-how-to-extract-certain-bit-from-string/#findComment-1192842 Share on other sites More sharing options...
QuickOldCar Posted March 27, 2011 Share Posted March 27, 2011 Here's how to get the position before too. <?php function getSecondLastPath($url) { $path = parse_url($url, PHP_URL_PATH); $trim_path = trim($path, '/'); $positions = explode('/', $trim_path); if (substr($path, -1) !== '/') { array_pop($positions); } return end($positions); } $insert_url = "http://natty.com/p/bh-fitness-class-indoor-magnetic-exercise-bike-2-years-parts-/detail/b004r2wuak/fitness-spinning.html"; echo getSecondLastPath($_SERVER['REQUEST_URI']);//get current page echo '<br />'; echo getSecondLastPath($insert_url); echo '<br />'; ?> Demo: http://get.blogdns.com/dynaindex/find-second-last-path.php Link to comment https://forums.phpfreaks.com/topic/231849-how-to-extract-certain-bit-from-string/#findComment-1192844 Share on other sites More sharing options...
yonny Posted March 27, 2011 Share Posted March 27, 2011 yeah that works better. Link to comment https://forums.phpfreaks.com/topic/231849-how-to-extract-certain-bit-from-string/#findComment-1192955 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.