ashleek007 Posted May 23, 2007 Share Posted May 23, 2007 Hi All, This is really bugging me.... I basically just want the regex for a name field in my form... as a check to make sure no user has input stupid characters... so the ones i would like are: - [a-z] and [A-Z] and 'spaces' and 'hyphens' and 'apostrophes' im currently using it as part of a preg_match and dont know the syntax of it all: - this is what ive got : - preg_match("/[a-z]+[A-Z]+/", $USER_VARS['FORE']) can someone throw a light on it at least? Cheers, Ash Quote Link to comment Share on other sites More sharing options...
obsidian Posted May 23, 2007 Share Posted May 23, 2007 Try this: <?php preg_match("/^[a-z' -]+\z/i", $USER_VARS['FORE']); ?> Quote Link to comment Share on other sites More sharing options...
ashleek007 Posted May 23, 2007 Author Share Posted May 23, 2007 Thanks alot for your help... can you explain what that does?... it doesnt seem to give the desired result... cheers, Ash Quote Link to comment Share on other sites More sharing options...
ashleek007 Posted May 23, 2007 Author Share Posted May 23, 2007 sorry it does seem to do what I wanted... just need to put a ! infront of the preg_match for my purpose... I've just realised though that my DB doesnt like apostrophes... is this normal? preg_match("/^[a-z -]+\z/i", $USER_VARS['FORE']); would that be the code if I didnt want to allow apostrophes?? Cheers, Ash Quote Link to comment Share on other sites More sharing options...
obsidian Posted May 23, 2007 Share Posted May 23, 2007 I've just realised though that my DB doesnt like apostrophes... is this normal? This is because you need to escape apostrophes before you insert into your DB. So, you'll need to run your string through mysql_real_escape_string() to clean it up before your actual INSERT statement. Read upon SQL Injection to see why this is such an issue. preg_match("/^[a-z -]+\z/i", $USER_VARS['FORE']); would that be the code if I didnt want to allow apostrophes?? Sure is. Here's a quick overview of what the code is doing: / = starting delimiter for my regexp ^ = match starting at the beginning of the string [ = start a character set a-z '- = alpha characters, spaces, apostrophes and dashes ] = end the character set + = must have one or more of the character set defined \z = match ending at the end of the string / = ending delimiter for my regexp i = declares this as a case INsensitive search Hope this helps! Quote Link to comment Share on other sites More sharing options...
ashleek007 Posted May 23, 2007 Author Share Posted May 23, 2007 thanks loads.... Ill have a go at the escaping see if I can figure it out... Cheers again, Ash Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.