another regex question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • J. Frank Parnell

    another regex question

    user creates a new folder thru form:

    $foldername = stripslashes ( $_POST['foldername'] );
    //This erase white-spaces on the beginning and the end in each line of a
    string:
    $foldername = preg_replace('~ ^(\s*)(.*?)(\s* )$~m', "\\2", $foldername);
    //erases all NON-alfanumerics
    $foldername = ereg_replace("[^[:alnum:]+]","-",$folderna me);

    this is working ok, but i cant figure out how to allow spaces between words
    (ereg_replace("[^[:alnum:]+]","-", is inserting dashes)

    thanks,
    j


  • John Dunlop

    #2
    Re: another regex question

    J. Frank Parnell wrote:

    [ ... ]
    [color=blue]
    > this is working ok, but i cant figure out how to allow spaces between words
    > (ereg_replace("[^[:alnum:]+]","-", is inserting dashes)[/color]

    Substitute a space for the plus sign, giving you

    ereg_replace('[^[:alnum:] ]','-',$subject)

    Note, though, that as it was, your pattern matched any *one*
    character excluding alphanumerics and the plus sign itself,
    not any characters except one or more alphanumerics. That
    is, the plus sign wasn't a metacharacter; it had no special
    meaning.

    --
    Jock

    Comment

    • Chung Leong

      #3
      Re: another regex question


      "J. Frank Parnell" <JFrank@plateof shrimpp.com> wrote in message
      news:KcedndOqPq EfcbjfRVn-vg@comcast.com. ..[color=blue]
      > user creates a new folder thru form:
      >
      > $foldername = stripslashes ( $_POST['foldername'] );
      > //This erase white-spaces on the beginning and the end in each line of a
      > string:
      > $foldername = preg_replace('~ ^(\s*)(.*?)(\s* )$~m', "\\2", $foldername);
      > //erases all NON-alfanumerics
      > $foldername = ereg_replace("[^[:alnum:]+]","-",$folderna me);
      >
      > this is working ok, but i cant figure out how to allow spaces between[/color]
      words[color=blue]
      > (ereg_replace("[^[:alnum:]+]","-", is inserting dashes)
      >
      > thanks,
      > j
      >
      >[/color]

      preg_replace('/[^\w\x20]+/', '', trim($folder)) should do the trick.


      Comment

      • J. Frank Parnell

        #4
        Re: another regex question


        "John Dunlop" <usenet+2004@jo hn.dunlop.name> wrote in message
        news:MPG.1c904e 4839740a2398988 3@News.Individu al.NET...[color=blue]
        > J. Frank Parnell wrote:
        >
        > [ ... ]
        >[color=green]
        >> this is working ok, but i cant figure out how to allow spaces between
        >> words
        >> (ereg_replace("[^[:alnum:]+]","-", is inserting dashes)[/color]
        >
        > Substitute a space for the plus sign, giving you
        >
        > ereg_replace('[^[:alnum:] ]','-',$subject)
        >
        > Note, though, that as it was, your pattern matched any *one*
        > character excluding alphanumerics and the plus sign itself,
        > not any characters except one or more alphanumerics. That
        > is, the plus sign wasn't a metacharacter; it had no special
        > meaning.[/color]

        Thanks, i thought i had tried every possible combination. And i was under
        the impression that the + was 'means "match one or more of the previous
        expression", and i had many combinations with that + sign that caused the
        alnum-only thing to not work...

        also, i added this
        $foldername = preg_replace('/\s\s+/', ' ', $foldername); to get rid of
        excess spaces. so, now i have:

        $foldername = stripslashes ( $_POST['foldername'] );
        $foldername = preg_replace('~ ^(\s*)(.*?)(\s* )$~m', "\\2", $foldername);
        $foldername = ereg_replace("[^[:alnum:] ]","-",$folderna me);
        $foldername = preg_replace('/\s\s+/', ' ', $foldername);

        which, as far as i can tell, makes appropriate folder names.

        thanks again, j



        Comment

        Working...