SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding/Removing "/" from links

    I'm developing a website with sections on animals and plants that feature bread-crumb style links that look something like this:

    PHP Code:
    <?php include($_SERVER['DOCUMENT_ROOT'] . '/a1/inc/articles/plants/' .$myclass '/' .$myorder '/' $myfamily '/' .$mycode '.php'); ?>
    But I often run into situations that require one more or less segment, frequently leaving me with links that display like this:

    /animals/mammals//carnivores/cats/

    So I need a script that will replace every instance of a double slash - // - with a single slash. In fact, I'd also like to know how to both add and delete a single slash as well, as I think I may have need of that, too.

    Thanks.

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    preg_replace?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ
    preg_replace?
    How would you write that to replace two slashes with a single slash?

    I just found a string replace function in one of my scripts that might work, also:

    PHP Code:
    $foo = array("us-""ca-");
    $mycode str_replace($foo""$mycode); 
    But I'm not sure exactly how to apply it. Maybe something like this?:

    PHP Code:
    $foo = "//";

    <?php include($_SERVER['DOCUMENT_ROOT'] . '/a1/inc/articles/plants/' .$myclass '/' .$myorder '/' $myfamily '/' .$mycode '.php'); ?> 

    $mylink = str_replace($f00, "", $toplink)

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    Should work - try it!

    SpikeZ
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ
    Should work - try it!

    SpikeZ
    No, I did something wrong.

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    you could try something like:

    $mystring = str_replace("//", "/", $string);

    not sure how this would work with the include construct though. I will give it some thought.

    SpikeZ
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Wizard Crowe's Avatar
    Join Date
    Nov 2001
    Location
    Huntsville
    Posts
    1,117
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    spike, your solution is right. I would do it like this:

    PHP Code:
    <?php
    $include 
    $_SERVER['DOCUMENT_ROOT'] . '/a1/inc/articles/plants/' .$myclass '/' .$myorder '/' $myfamily '/' .$mycode '.php'

    $include str_replace("//""/"$include);

    include(
    $include);

    ?>
    Chrispian H. Burks
    Nothing To Say

  8. #8
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you know that you'll never have more than 2 consecutive slashes, use the str_replace solution above. however, if you run it against '///', you will still have '//' remaining. here's the regular expression to use for preg_replace. it will match any pattern of 2 or more consecutive slashes. use preg_replace to replace with a single slash
    Code:
    /[/]{2,}/

  9. #9
    SitePoint Wizard Crowe's Avatar
    Join Date
    Nov 2001
    Location
    Huntsville
    Posts
    1,117
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Darchangel, good catch. I bet he could end up with /// because if more than 2 sections are omitted then their will be 3 stacked up.
    Chrispian H. Burks
    Nothing To Say


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •