SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP regex problem...

    Okay, done trying to find this via google...

    I am trying to remove a forward slash from a string received from the query string. I have the following code that according to my research should work, but isn't. The string looks like this:

    text/

    Here is the code I am trying to use to do this...
    ( I have tried using " too. )

    ereg_replace('/','',$string);

    Could it be a version problem?

    Any help?

    Thanks,
    flx

  2. #2
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you only want to replace all occurrences of / then you certainly don't want to be using regexes

    PHP Code:
    <?php

        $str 
    'test/';

        echo 
    str_replace '/'''$str );

    ?>
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's amazing how much time you waste in the wee hours of the morning on stuff... str_replace never came up in my research.

    Of course, my problem was more related to me now that have figured it out.

    $string = str_replace('/','',$string);

    !

    I left out above that I was trying to assign the manipulated value back to the string. ( I never really found that in my research either... of course, I wasn't really searching for that. )

    Basically, I usually code in Perl and am trying to port 1 script to ASP and PHP... I just didn't really make the connection that I should set the value to the string. In Perl... once you do the regex... it's done.

    Oh well, thanks Sean!

    -flx

  4. #4
    SitePoint Enthusiast Viper_SB's Avatar
    Join Date
    Aug 2003
    Location
    Canada
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just so you know preg_replace() is faster (and easier) to use then ereg, preg (perl compatible regex) uses the same sytax as regex in perl, you might find it easier in the long run then having to learn how to use ereg.

    Also if you really only want it removed fromt the front of the string use rtrim()
    PHP Code:
    $str 'test/';
    print 
    rtrim($test'/'); 


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
  •