SitePoint Sponsor

User Tag List

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

    Dynamic URL str_replace

    I'm working on a PHP script that displays breadcrumbs-type top navigation links. It works OK for the most part, but there's one bug I can't figure out how to fix. Links to the home pages of various sections are displayed twice instead of once. For example, what should be http://MySite/People is http://MySite/People/People.

    The following fix works by removing the second instance of People:

    PHP Code:
    echo "<a href=\"/People/".str_replace('People'''$mypath[$i])."\"> ".str_replace('_'' '$mypath[$i])."</a>&nbsp;&gt;"
    But it's a sloppy fix in that it will presumably remove ALL instances of People; what if I want to link to http://MySite/People/Rich_People? I'd be left with MySite/People/Rich_. Instead, I simply want to replace instances of People/People with People.

    I thought the following might work if I could somehow wrap the entire URL - including the prefix /People/ - inside it:

    str_replace('People/People', 'People/', [STRING TO BE REPLACED])

    But I can't figure out how to implement it. Can anyone show me how to write it, or suggest a better idea?

    Thanks.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Assuming the code snippet you gave is what you're actually using, there's only one People/ in the input where two appear as the other People/ in the URL comes from your echo statement.

    So, you only want to replace one instance of People/, not People/People:

    PHP Code:
    echo "<a href=\"/People/" str_replace('People/'''$mypath[$i]) . "\"> " str_replace('_'' '$mypath[$i]) . "</a>&nbsp;&gt;"

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see what you're trying to do, but it doesn't work for me for some reason. This does work

    PHP Code:
    echo "<a href=\"/People/".str_replace('People'''$mypath[$i])."\">

    // etc.
     "
    .str_replace('_'' '$mypath[$i])."</a>&nbsp;&gt;"
    This does work...

    <a href=\"/People/".str_replace('People', '', $mypath[$i])."\">

    ...but not if the second instance of "People" is preceded or followed by a slash. Again, that might suffice for the time being, but I won't be able to add any links that contain the word "People" unless I can somehow zap only "People/People."

    But another idea just popped into my head...Suppose I convert my link to a variable - something like this:

    PHP Code:
    $Link '<a href=\"/People/".str_replace('People', '', $mypath[$i])."\">'
    Then I could replace my display with this, right?:

    str_replace('People/People', 'People', $Link);

    Can anyone show me how to write that? I'm working on it right now, but I think it's going to be a wrestling match.

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What I wrote is tested:
    PHP Code:
    <?php
    $string 
    "People/Test";
    echo 
    $string "<br />";
    echo 
    str_replace('People/'''$string);
    ?>
    Output:
    Quote Originally Posted by http://v3.w3counter.com/sptest/str.php
    People/Test
    Test
    Yes, what you wrote will work, so will what's already been written.

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmmm...I'll have to take a closer look and see what I'm doing wrong. In the meantime, I figured out how to wrap the entire thing in a str_replace function:

    PHP Code:
    $AAA '<a href="/People/'.$mypath[$i].'">';

    echo 
    "".str_replace('People/People''People/'$AAA)." ".str_replace('_'' '$mypath[$i])."</a>&nbsp;&gt;"
    I'll have to do some more testing, but it seems to work OK. Thanks for the tips and the link.


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
  •