Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: how to remove this string using regex

  1. #1
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question how to remove this string using regex

    look,
    i have a variable that contain dynamic value,

    for example i have $url,
    sometimes, this $url contain a value : http://mysite.com/index.php
    sometimes, this $url contain a value : http://mysite.com/index.php/login/

    i just want to replace "index.php" and "index.php/"
    so the result like below:
    http://mysite.com/
    http://mysite.com/login/

    i have just used str_replace("index.php", "",$url) but it doesnt solve my problem
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Try:

    PHP Code:
    $url str_replace("index.php"""$url); 
    - Josh

  3. #3
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Your code just result like below
    http://mysite.com/
    http://mysite.com//login/

    i want like this
    http://mysite.com/
    http://mysite.com/login/

    how can i do this?
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  4. #4
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Wait, why didn't you just add the forward slash in the removal attribute?

    Try this?
    PHP Code:
    $url str_replace("/index.php"""$url); 
    - Josh

  5. #5
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Your last code will be like below
    http://mysite.com
    http://mysite.com/login/

    but i want the code like below
    http://mysite.com/
    http://mysite.com/login/

    :'(
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  6. #6
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    ????

    Try this...
    PHP Code:
    $url str_replace("index.php/"""$url); 
    Please read this. It will teach you how to use str_replace().
    http://php.net/str_replace
    - Josh

  7. #7
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  8. #8
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Try, this will do any address not just login

    PHP Code:
    $url preg_replace("/(.*?\.com/)(.*)\..*/""$1$2/"$url); 
    Corrections to my coding/thoughts welcome.

  9. #9
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,689
    Thanks
    82
    Thanked 89 Times in 87 Posts

    Default

    You may want to test your pcre before posting bluewalrus. Or if you are not going to test then say that you have not tested it yet.

    I have not done pcre in a while, but try the following:

    Code:
    <?php
    $url="http://mysite.com/login/";
    $url=preg_replace('/(http:\/\/mysite\.com)(.*?(\/login\/))/',"$1$3",$url);
    echo "$url";
    ?>
    I know the above is very sloppy, but I am not aware of all of the parameters of smansakra's $url variable.

    This code makes several assumptions about what is contained in $url, but try it out and if you need what I posted to be modified let us know.
    Last edited by james438; 08-27-2011 at 07:28 PM.
    To choose the lesser of two evils is still to choose evil. My personal site

  10. #10
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    @bluewalrus and @james348: Thanks for reply i have tried both of your code, but doesn't work,

    this is my code:
    PHP Code:
    <?
    function menu_top(){
        
    $domain "http://www.mysite.ext"//can be mysite.com, mysite.us, mysite.co.id, mysite.web.id, and so on
        
    $menu = array(
            
    'Home' => $domain,
            
    'People' => $domain.'/people/',
            
    'News' => $domain.'/news/',
            
    'Blog' => $domain.'/blog/'
        
    );
        
    $current_page $domain.$_SERVER['PHP_SELF'];
        
    //this current page always return something like http://www.mysite.ext/index.php/mypage/
        //sometimes return http://www.mysite.ext/index.php
        //
        //home link, in menu shows http://www.mysite.ext/ , but in current_page return http://www.mysite.ext/index.php
        //people link, in menu show http://www.mysite.ext/people/, but in current_page return http://www.mysite.ext/incex.php/people/
        //and the other link are too
        //
        /*
         so, i want a regex to remove index.php from $current_page,
         like:
         http://mysite.com/
         http://mysite.com/people/
        
         not like:
         http://mysite.com/
         http://mysite.com//people/ 
         
         not like:
         http://mysite.com
         http://mysite.com/people/
        */
        
    $ret ='<ul>';
        foreach(
    $menu as $link_text => $link_url){
            
            if( 
    $current_page == $link_url)
                
    $ret .='<li class="link_active">'.$link_text.'</li>';
            else
                
    $ret .='<li><a href="'.$link_url.'">'.$link_text.'</a></li>';
        }
        
    $ret .='</ul>';
       
        return 
    $ret;
    }
    ?>
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

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
  •