another regex question

This is a discussion on another regex question within the PHP Language forums, part of the PHP Programming Forums category; user creates a new folder thru form: $foldername = stripslashes ( $_POST['foldername'] ); //This erase white-spaces on the beginning and the ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-02-2005
J. Frank Parnell
 
Posts: n/a
Default 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:]+]","-",$foldername);

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

thanks,
j


Reply With Quote
  #2 (permalink)  
Old 03-02-2005
John Dunlop
 
Posts: n/a
Default Re: another regex question

J. Frank Parnell wrote:

[ ... ]

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


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
Reply With Quote
  #3 (permalink)  
Old 03-03-2005
Chung Leong
 
Posts: n/a
Default Re: another regex question


"J. Frank Parnell" <JFrank@plateofshrimpp.com> wrote in message
news:KcedndOqPqEfcbjfRVn-vg@comcast.com...
> 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:]+]","-",$foldername);
>
> this is working ok, but i cant figure out how to allow spaces between

words
> (ereg_replace("[^[:alnum:]+]","-", is inserting dashes)
>
> thanks,
> j
>
>


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


Reply With Quote
  #4 (permalink)  
Old 03-03-2005
J. Frank Parnell
 
Posts: n/a
Default Re: another regex question


"John Dunlop" <usenet+2004@john.dunlop.name> wrote in message
news:MPG.1c904e4839740a23989883@News.Individual.NE T...
> J. Frank Parnell wrote:
>
> [ ... ]
>
>> this is working ok, but i cant figure out how to allow spaces between
>> words
>> (ereg_replace("[^[:alnum:]+]","-", is inserting dashes)

>
> 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.


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:] ]","-",$foldername);
$foldername = preg_replace('/\s\s+/', ' ', $foldername);

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

thanks again, j



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:41 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0