473,385 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Jul 17 '05 #1
3 1159
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
Jul 17 '05 #2

"J. Frank Parnell" <JF****@plateofshrimpp.com> wrote in message
news:Kc********************@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.
Jul 17 '05 #3

"John Dunlop" <us*********@john.dunlop.name> wrote in message
news:MP************************@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

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lawrence | last post by:
When users enter urls or other long strings it can destroy the formatting of a page. A long url, posted in a comment, can cause page distortions that make the page unreadable, till the website...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
by: Tim Conner | last post by:
Hi, Thanks to Peter, Chris and Steven who answered my previous answer about regex to split a string. Actually, it was as easy as create a regex with the pattern "/*-+()," and most of my string...
6
by: Du Dang | last post by:
Text: ===================== <script1> ***stuff A </script1> ***more stuff <script2> ***stuff B
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
4
by: Chris | last post by:
Hi Everyone, I am using a regex to check for a string. When all the file contains is my test string the regex returns a match, but when I embed the test string in the middle of a text file a...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
5
by: redamazon200 | last post by:
I am looking for a way to copy a pattern (letter 'A' in the following example) to another string. string str1 = "1111AAAA111111AA"; string str2 = "1111000000001111"; After the copy str2...
3
by: =?Utf-8?B?bWFnZ2ll?= | last post by:
hi, I've been working getting a file parsed out using Regex. There's something I don't understand. When I define the pattern for my fields in my file, I am telling regex to grab those fields (...
6
by: | last post by:
Hi all, Sorry for the lengthy post but as I learned I should post concise-and-complete code. So the code belows shows that the execution of ValidateAddress consumes a lot of time. In the test...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.