misterm Posted August 21, 2008 Share Posted August 21, 2008 Hi Can anyone help with this? If the url of a certain page on my site is http://www.mysite.com/category/books/345 I use <? $thisPage = $_SERVER['REQUEST_URI']; ?> to set $thisPage as "http://www.mysite.com/category/books/345" What I really need to do now is set the variable $thisCategory to whatever comes between category/ and /345 keeping in mind that the number at the end will change for every category of the site. The reason I can't just grab an existing variable for the category is because it is hissed in an ioncube encrypted file. Any help would be so appreciated as this would save me hours and hours of work. many thanks Mr M Link to comment https://forums.phpfreaks.com/topic/120703-how-to-extract-specific-info-from-the-url/ Share on other sites More sharing options...
wayjo Posted August 21, 2008 Share Posted August 21, 2008 Hiyah, I think I would try using regular expression to do that. I don't think there is a function to do it for you. Just do a search for php and regular expression in google and you'll get loads of results and examples. PHP covers two types of regexp in the help file too. HTH! wayjo Link to comment https://forums.phpfreaks.com/topic/120703-how-to-extract-specific-info-from-the-url/#findComment-621960 Share on other sites More sharing options...
abdfahim Posted August 21, 2008 Share Posted August 21, 2008 why dont you try this <?php $thisPage = $_SERVER['REQUEST_URI']; $temp1=explode("category",$thisPage); $temp2=explode("/",$temp1); $thisCategory=$temp2[1]; ?> Link to comment https://forums.phpfreaks.com/topic/120703-how-to-extract-specific-info-from-the-url/#findComment-621968 Share on other sites More sharing options...
The Little Guy Posted August 21, 2008 Share Posted August 21, 2008 If it will always be formatted the same, you could do this: $url = 'http://www.mysite.com/category/books/345'; $values = explode('/',$url); print_r($values); Link to comment https://forums.phpfreaks.com/topic/120703-how-to-extract-specific-info-from-the-url/#findComment-621972 Share on other sites More sharing options...
misterm Posted August 21, 2008 Author Share Posted August 21, 2008 Thanks Little Guy That outputs: Array ( [0] => [1] => Category [2] => Branding [3] => 159 ) But am not sure how to use it to get "Branding". :/ Yes, the format will lways be the same. If it will always be formatted the same, you could do this: $url = 'http://www.mysite.com/category/books/345'; $values = explode('/',$url); print_r($values); Link to comment https://forums.phpfreaks.com/topic/120703-how-to-extract-specific-info-from-the-url/#findComment-621978 Share on other sites More sharing options...
misterm Posted August 21, 2008 Author Share Posted August 21, 2008 Thanks abdbuet That just seems to output the full page url :/ why dont you try this <?php $thisPage = $_SERVER['REQUEST_URI']; $temp1=explode("category",$thisPage); $temp2=explode("/",$temp1); $thisCategory=$temp2[1]; ?> Link to comment https://forums.phpfreaks.com/topic/120703-how-to-extract-specific-info-from-the-url/#findComment-621981 Share on other sites More sharing options...
The Little Guy Posted August 21, 2008 Share Posted August 21, 2008 $url = 'http://www.mysite.com/category/books/345'; $values = explode('/',$url); echo $values[4]; Link to comment https://forums.phpfreaks.com/topic/120703-how-to-extract-specific-info-from-the-url/#findComment-621986 Share on other sites More sharing options...
misterm Posted August 21, 2008 Author Share Posted August 21, 2008 Brilliant!!! Thank you so much. That works a treat. And thanks again abdbuet for your suggestion. Mr M $url = 'http://www.mysite.com/category/books/345'; $values = explode('/',$url); echo $values[4]; Link to comment https://forums.phpfreaks.com/topic/120703-how-to-extract-specific-info-from-the-url/#findComment-621995 Share on other sites More sharing options...
kenrbnsn Posted August 21, 2008 Share Posted August 21, 2008 You can also do something like: <?php $url = 'http://www.mysite.com/category/books/345'; $purl = parse_url($url); list ($dmy,$catagory,$type,$id) = explode('/',$purl['path']); print_r($purl); echo '$catagory: ' . $catagory . "\n"; echo '$type: ' . $type . "\n"; echo '$id: ' . $id . "\n"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/120703-how-to-extract-specific-info-from-the-url/#findComment-622011 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.