pixeltrace Posted March 20, 2009 Share Posted March 20, 2009 Hi, I need help, i need to grab only a certain name from a url using php but i dont have any idea on how to do this. example: http://www.domainname.com/0903/filename.php?id=1 i only need to get "0903" hope you could help me with this. thanks so much! Link to comment https://forums.phpfreaks.com/topic/150279-solved-how-to-grab-certain-word-from-url/ Share on other sites More sharing options...
wmguk Posted March 20, 2009 Share Posted March 20, 2009 will www.domainname.com always be the same? Link to comment https://forums.phpfreaks.com/topic/150279-solved-how-to-grab-certain-word-from-url/#findComment-789204 Share on other sites More sharing options...
wmguk Posted March 20, 2009 Share Posted March 20, 2009 if so, $url = $_SERVER["REQUEST_URI"]; $url = substr("$url", 1, 4); // returns "0903" echo $url; Starts after the domainname.com/ and shows the next 4 charactors... this is on the basis that the code you need is only 4 charactors long Link to comment https://forums.phpfreaks.com/topic/150279-solved-how-to-grab-certain-word-from-url/#findComment-789213 Share on other sites More sharing options...
redarrow Posted March 20, 2009 Share Posted March 20, 2009 geek way. <?php $url="http://www.domainname.com/0903/filename.php?id=1"; if(preg_match_all("/[0-9]{4}/",$url,$matched)){ PRINT_R($matched); } ?> result Array ( [0] => Array ( [0] => 0903 ) ) encase need the variable for anything. <?php $url="http://www.domainname.com/0903/filename.php?id=1"; if(preg_match_all("/[0-9]{4}/",$url,$matched)){ $result=$matched[0][0]; } echo $result; ?> Link to comment https://forums.phpfreaks.com/topic/150279-solved-how-to-grab-certain-word-from-url/#findComment-789214 Share on other sites More sharing options...
pixeltrace Posted March 20, 2009 Author Share Posted March 20, 2009 Thanks for all the replies. its working now! Link to comment https://forums.phpfreaks.com/topic/150279-solved-how-to-grab-certain-word-from-url/#findComment-789226 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.