Exclude a

Discussion in 'PHP' started by 123GoToAndPlay, Mar 17, 2010.

  1. #1
    hi

    I am using this simple breadcrumb function but i like to exclude certain segments

    
    function breadcrumbs(){
      $startAtCrumb =3;
      $url = 'http://www.mydomain.com/projectz/breadcrumbs/';
      $bread = explode('/', $_SERVER['REQUEST_URI']);
    //$excludeSegments
      $returnString = '<div id="breadcrumbs"><a href='.$url.'>Home</a>';
      for($i=$startAtCrumb;$i<count($bread)-1;$i++){
        $url.=$bread[$i].'/';
        $returnString .= ">> </span> <span class='bc$i'><a href='$url'>"
          .prettify($bread[$i])."</a>";
      }
      echo $returnString.'</div>';
    }
    
    Code (markup):
    I have this solution now
    
     $exclude = str_replace('category/','',$_SERVER['REQUEST_URI']);
      $bread = explode('/', $exclude);
    
    Code (markup):
    But i think i could be better, any suggestions??
     
    123GoToAndPlay, Mar 17, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There's not really anything out there more generic than that. One thing you could do is pass an array of different breadcrumbs, and also use regular expressions to determine that the full breadcrumb is being taken away. For example, in this string
    /category/subcategory/product-name.html
    you would end up with
    /subproduct-name.html
    If you use lookaround characters to verify that the character before the start of the crumb name is a /
    example regex:
    '%(?<=/)category/%'
     
    JAY6390, Mar 17, 2010 IP
  3. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    @jay6390, tx for your reply, i am stupid when it comes to regex ;)

    but i see what you mean with your regex example. At the moment i know category is the only one to exclude but you are indeed correct with the subcategory issue.
    In my case changing str_replace to
    
     $exclude = str_replace('/category','',$_SERVER['REQUEST_URI']);
    
    Code (markup):
    should do the trick, right??
     
    123GoToAndPlay, Mar 18, 2010 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well yes it will work ok for the subcategory, but what about
    /category/category1/ will change it to
    1/
    which is the same problem all over again. regex is definitely the way to go
     
    JAY6390, Mar 18, 2010 IP