robbluther Posted July 1, 2008 Share Posted July 1, 2008 Here is my question... I want to assign a specific style based on part of the URL. So, if my URLs start: /section_page, I want to know the /section and assign a style to it. I am using: <style type="text/css" media="screen"> @import "../styles/nav.php"; </style> to Import my PHP style... I need to know how to rip apart the URL and test for the "section". Any help would be appreciated. Thanks! Link to comment https://forums.phpfreaks.com/topic/112767-style-based-on-url-with-_-seperator/ Share on other sites More sharing options...
JD* Posted July 1, 2008 Share Posted July 1, 2008 You should look up on exploding strings into arrays: http://us3.php.net/manual/en/function.explode.php You can use that to do: $array = explode("/", $_SERVER['REQUEST_URI']); then access the $array[1] or $array[2] or where ever your "section" part happens. Link to comment https://forums.phpfreaks.com/topic/112767-style-based-on-url-with-_-seperator/#findComment-579159 Share on other sites More sharing options...
robbluther Posted July 1, 2008 Author Share Posted July 1, 2008 That will pull the URL... but how do I test for the _ separator? Thanks for the fast response btw... Link to comment https://forums.phpfreaks.com/topic/112767-style-based-on-url-with-_-seperator/#findComment-579174 Share on other sites More sharing options...
rhodesa Posted July 1, 2008 Share Posted July 1, 2008 just explode on the _ $page = 'section_page'; $parts = explode('_',$page); Link to comment https://forums.phpfreaks.com/topic/112767-style-based-on-url-with-_-seperator/#findComment-579179 Share on other sites More sharing options...
robbluther Posted July 1, 2008 Author Share Posted July 1, 2008 Thanks guys! I appreciate the help... Here is the code I used if anyone else needs help. <?php $page = $_SERVER['PHP_SELF']; $parts = explode('_',$page); print_r ($parts[0]); ?> Link to comment https://forums.phpfreaks.com/topic/112767-style-based-on-url-with-_-seperator/#findComment-579189 Share on other sites More sharing options...
robbluther Posted July 1, 2008 Author Share Posted July 1, 2008 Ok... one more issue... I am trying to post this code in my .php file to manipulate my DIV classes... when it is in the head or the body my page does not load at all. <?php $page = $_SERVER['PHP_SELF']; $parts = explode('_',$page); if($parts[0] == '/traditional.php' || '/traditional'){ $style = 'activeList'; }elseif{ ($parts[0] == '/utility.php' || '/utility'){ $style = 'activeList'; }else{ $style = ''; } ?> Thanks again guys! <!------------------------- UPDATE - Sorry it was a stupid Typo -------------------------------------> Link to comment https://forums.phpfreaks.com/topic/112767-style-based-on-url-with-_-seperator/#findComment-579267 Share on other sites More sharing options...
robbluther Posted July 1, 2008 Author Share Posted July 1, 2008 Can I use multiple string separators? From this: <?php $page = $_SERVER['PHP_SELF']; $parts = explode('_',$page); print_r ($parts[0]); ?> to this? <?php $page = $_SERVER['PHP_SELF']; $parts = explode('_', '.' ,$page); print_r ($parts[0]); ?> Link to comment https://forums.phpfreaks.com/topic/112767-style-based-on-url-with-_-seperator/#findComment-579277 Share on other sites More sharing options...
rhodesa Posted July 1, 2008 Share Posted July 1, 2008 not with explode...but you can with preg_split: <?php $page = $_SERVER['PHP_SELF']; $parts = preg_split("/[_.]/",$page); print_r($parts[0]); ?> Link to comment https://forums.phpfreaks.com/topic/112767-style-based-on-url-with-_-seperator/#findComment-579302 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.