small php code help needed

Discussion in 'PHP' started by himtuna, Mar 21, 2009.

  1. #1
    Hi
    I need help with the php code

    <?php
    $current_url = "http://" .$_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI'];
    print $current_url
    ?>

    this is gonna return a url ( a string ) with exactly this pattern ( so dont have to worry much about the logics fo the url returned)
    http://yoursite.com/node/123/idontwant_thisstuff
    or could be
    http://yoursite.com/?q=node/123/idontwant_thisstuff

    Basically i want to run a loop to get rid of "/idontwant_thisstuff" i.e anything after node/87/...
    should get deleted. i.e i just want url two main things
    first the word "node"( ofcourse the base url also http://yoursite.com/ ) and the numeric value proceeding it.

    eg http://yoursite.com/node/1/events ---> http://yoursite.com/node/1
    http://yoursite.com/node/123423423/groups -----> http://yoursite.com/node/123423423

    please if someone could do this
    Having working since morning on drupal, and this is the last step(except some css work) to achieve my goals.
    please help me out
    Thanks
    Himtuna
     
    himtuna, Mar 21, 2009 IP
  2. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #2
    I am not sure you are pulling the right information to do what you want and I do not have enough experience to write the code from scratch without testing it.

    I think a better approach might be contained in this script fragment which I think does what does what you want. Remove the remarks (//) to debug it for your use.

    Post back and let us know if that worked.
     
    Colbyt, Mar 21, 2009 IP
  3. Joak1m

    Joak1m Peon

    Messages:
    135
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <?php
    $url = "$_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI'];"
    $str = explode("/",$url);
    echo "$str[1]/$str[2]/$str[3]";
    ?> 
    PHP:
     
    Joak1m, Mar 21, 2009 IP
  4. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #4
    I used this (drupal specific)

    
    <?php
    if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
      $my_id = arg(1);
      echo $my_id ; 
    }
    
    ?>
    
    Code (markup):
    All you would have to do is echo the domain and ?node= . Course will require php formatting in drupal. And I'm not sure if it works with extra crud on the query string.
     
    shallowink, Mar 21, 2009 IP
  5. himtuna

    himtuna Member

    Messages:
    225
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #5
    Thanks a lot to every one.
    @shallowink
    Your code does what I wanted.
    How to get the base url of the the drupal
     
    himtuna, Mar 21, 2009 IP
  6. himtuna

    himtuna Member

    Messages:
    225
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #6
    This is what I was looking for.
    Thanks
     
    himtuna, Mar 21, 2009 IP