Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: how to remove this string using regex

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

    Default

    Untested in PHP.

    PHP Code:
    $url preg_replace('/' $domain '.*?\/(.*)\/'$domain '/$1/'$current_page); 
    Corrections to my coding/thoughts welcome.

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

    Default

    i have tried it,
    function.preg-replace ]: Unknown modifier '/'
    :'(
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  3. #13
    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);  
    $url str_replace("//""/"$url); 
    - Josh

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

    Default

    Opps, forgot to close the last one

    PHP Code:
    $url preg_replace('/' $domain '.*?\/(.*)\//'$domain '/$1/'$current_page); 
    That should do it.
    Corrections to my coding/thoughts welcome.

  5. #15
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,260
    Thanks
    96
    Thanked 104 Times in 102 Posts

    Default

    I am terrible with functions, but try this:

    Code:
    <?php
    $url="http://mysite.com";
    $url=preg_replace('/\/index\.php\/?$/',"/",$url);
    $url=preg_replace('/\/index\.php\/?/',"/",$url);
    $url=preg_replace('/([a-zA-Z])$\/?/',"$1/",$url);
    echo "$url";
    ?>
    Jshor, what you propose would also turn http://mysite.com/ into http:/mysite.com/. It still might be possible to do this with str_replace, but I'm not sure how yet.
    To choose the lesser of two evils is still to choose evil. My personal site

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

    Default

    @james, bluwalrus, jshor: Thanks for all reply from you guys,
    i have found my self
    my be a little poor,

    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_php_self str_replace('index.php/'''$_SERVER['PHP_SELF']);
            
    $current_php_self str_replace('index.php'''$current_php_self);

            
    $current_page $domain.$current_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
    ///////////////////////////////////////////////////

  7. #17
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,260
    Thanks
    96
    Thanked 104 Times in 102 Posts

    Default

    Ah, clever girl . I can't see anything wrong with that and it is so simple too.
    To choose the lesser of two evils is still to choose evil. My personal site

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
  •