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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
"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. |
|
|||
|
"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 |