Need help with split string...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
123dhs321
Forum Newbie
Posts: 5
Joined: Mon Jan 24, 2005 11:53 am

Need help with split string...

Post by 123dhs321 »

I need to switch a style based on which directory your in.

I have one more question if you have time. By the way I’m a beginner at php. :)

This works with this url ….com/rideshare/:
<div id="<?php if ($_SERVER["REQUEST_URI"] == '/rideshare/') { echo 'ridesharetitle'; } else { echo 'hometitle'; }?>"</div>

Within the /rideshare/ folder I have dynamic premalinks gernerated by WP.
ex: ….com/rideshare/this-title/

How can I get the code
<div id="<?php if ($_SERVER["REQUEST_URI"] == '/rideshare/') { echo 'ridesharetitle'; } else { echo 'hometitle'; }?>"</div>
to include all the dynamic directories under the /rideshare/ directory? Hence, directories such as ….com/rideshare/this-title/ or ….com/rideshare/generated-title/ or ….com/rideshare/could-be-anything/

Site in development is here:
http://www.panictour.com/rideshare/

I recieved advise like this:

The logic for the last scenario is pretty much the same. You just need some function to split the string REQUEST_URI and place the substrings in an array. Something like:
<?php
list($directory, $subdirectory) = split('/', $_SERVER["REQUEST_URI"]);
echo '<div class="' . $directory . ' id="' . $subdirectory . '">';
?>

Thing is I dont know how to do this. Any help would be appreciated. I've been stuck on this for weeks now!

Thanks,
-Ryan
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

<div id="<?php if (strpos($_SERVER&#1111;"REQUEST_URI"],'/rideshare/') !== false) &#123; echo 'ridesharetitle'; &#125; else &#123; echo 'hometitle'; &#125;?>"</div>
is one way..
123dhs321
Forum Newbie
Posts: 5
Joined: Mon Jan 24, 2005 11:53 am

Post by 123dhs321 »

Works perfectly!


How would the code be accross many pages...

ie:

<div id="<?php if ($_SERVER["REQUEST_URI"] == '/rideshare/') { echo 'ridesharetitle'; } else ?php if ($_SERVER["REQUEST_URI"] == '/tourdates/') { echo 'tourdatestitle'; } else ?php if ($_SERVER["REQUEST_URI"] == '/ticketexchange/') { echo 'ticketexchangetitle'; } else { echo 'hometitle'; }?>"</div>

I know that may be wrong but you know what I'm saying... Thanks for the help so far.

-Ryan
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

if()
&#123;
...
&#125;
elseif(...)
&#123;
...
&#125;
elseif(...)
&#123;
...
&#125;
elseif(...)
&#123;
...
&#125;
else
&#123;
...
&#125;
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

or more efficiently:

Code: Select all

switch($_SERVER&#1111;"REQUEST_URI"])
&#123;
    case "rideshare":
        echo "ridesharetitle";
    break;

    case "tourdates":
        echo "tourdatestitle";
    break;
&#125;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

missing some logic for what he's doing... ;)
123dhs321
Forum Newbie
Posts: 5
Joined: Mon Jan 24, 2005 11:53 am

Post by 123dhs321 »

Is this right:

<?php if (strpos($_SERVER["REQUEST_URI"],'/rideshare/') !== false) { echo 'ridesharetitle'; } elseif (strpos($_SERVER["REQUEST_URI"],'/tourdates/') !== false) { echo 'tourdatestitle'; } elseif (strpos($_SERVER["REQUEST_URI"],'/ticketexchange/') !== false) { echo 'ticketexchangetitle'; } else { echo 'hometitle'; }?>



It doesnt seem to work. This syntax is hard. Alot harder than asp!

Thanks for the help so far.

-ryan
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

magicrobotmonkey illustrated an example of another way to do it.. there are many ways to do it in php.
123dhs321
Forum Newbie
Posts: 5
Joined: Mon Jan 24, 2005 11:53 am

Post by 123dhs321 »

I thought that magicrobotmonkey was missing the logic?

If not, how do I impliement that into my current code?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

oh right- woops. To do it my way, you would first have to pull the relevent bit out of the URI. Which if you could do it would be a lot better then a bunch of strpos()'s.
123dhs321
Forum Newbie
Posts: 5
Joined: Mon Jan 24, 2005 11:53 am

Post by 123dhs321 »

Can you give me an example based on my code? I'm really new to PHP.
Post Reply