daydreamer Posted August 27, 2008 Share Posted August 27, 2008 I need to validate a text area using php. I want to allow numbers, text, and these characters only: #=|/€$£^"&;_,?.@-)%+*\[]><]+$/ <?php if(!preg_match('/^[a-z0-9#=|/€$£^"&;_,?.@-)%+*\[]><]+$/'), $_POST['message']) ) ?> This is what i have so far but all i get is: Parse error: syntax error, unexpected ',' in C:\wamp\www\send.php on line 38 (the above line of code is line 38). Also the original php codes does not have those numbers €$£ in the string, the phpfreaks forum adds them for some reason. I dont have much experience using the preg_match function, any help would be good! cheers. Quote Link to comment https://forums.phpfreaks.com/topic/121568-preg_match-validation/ Share on other sites More sharing options...
discomatt Posted August 27, 2008 Share Posted August 27, 2008 You have an unnecessary ')' !preg_match('/^[a-z0-9#=|/€$£^"&;_,?.@-)%+*\[]><]+$/', $_POST['message']) You may also want to use the 'i flag for case insensitivity Quote Link to comment https://forums.phpfreaks.com/topic/121568-preg_match-validation/#findComment-626983 Share on other sites More sharing options...
nrg_alpha Posted August 27, 2008 Share Posted August 27, 2008 It seems you have a few inherent problems with your pattern. a) Notice this part of your pattern: @-: That dash, I assume is meant to be a dash.. but dashes in a character class need to be set first, otherwise, you are telling regex engine that this is a range (much like a-z). b) I think you need to escape your parethesis characters, as well as your square brackets.. otherwise regex thinks this is a group and a character class respectively. c) you need to escape your '/' character, because you are using these as delimiters as well. So here is the pattern corrected of those issues: if(!preg_match('/^[-a-z0-9#=|\/€$£^"&;_,?.@:\(\)%+*\[\]><]+$/'), $_POST['message'])) Quote Link to comment https://forums.phpfreaks.com/topic/121568-preg_match-validation/#findComment-627014 Share on other sites More sharing options...
daydreamer Posted August 27, 2008 Author Share Posted August 27, 2008 thanks for the case sensitive suggestion, i hadn't even noticed! ive taken the extra bracket away but same problem. I think it has something to do with the £. I now have this: if(!preg_match('/^[a-z0-9!?+&%\,\'@#() \$\"]+$/i', $_POST['message']) it works, but I would like to include £ and €. Also, another problem I have is that i need to skip white space, because when I press enter in the textfield this shows up as an error (because of the newline/white space). Ive read that you can use the s flag :"s - Includes New line". How do i do this with the i flag as well? Will this fix my problem? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/121568-preg_match-validation/#findComment-627023 Share on other sites More sharing options...
daydreamer Posted August 27, 2008 Author Share Posted August 27, 2008 thanks nrg_alpha, your explanation did help, but the validation still shows as error when i type £ and also $. I think most of the other symbols are fine though. one main problem i still have is when i type enter/return, that shows up as an error and i need to allow them. Quote Link to comment https://forums.phpfreaks.com/topic/121568-preg_match-validation/#findComment-627027 Share on other sites More sharing options...
nrg_alpha Posted August 27, 2008 Share Posted August 27, 2008 thanks nrg_alpha, your explanation did help, but the validation still shows as error when i type £ and also $. I think most of the other symbols are fine though. one main problem i still have is when i type enter/return, that shows up as an error and i need to allow them. You have those errors because you are missing those elements in your last snippet you provided.. try: if(!preg_match('/^[a-z0-9!?+&\/£%,\'@#()\$\"]+$/i', $str)) Quote Link to comment https://forums.phpfreaks.com/topic/121568-preg_match-validation/#findComment-627109 Share on other sites More sharing options...
thebadbad Posted August 27, 2008 Share Posted August 27, 2008 This should work, and allows linebreaks (by removing them before the validation): <?php $_POST['message'] = str_replace("\n", '', str_replace("\r", '', $_POST['message'])); if (!preg_match('~^[-0-9a-z#=|/€$£"&;_,?.@)%+*^[\]>\\\<]+$~iD', $_POST['message'])) { echo 'Error!'; } ?> A backslash is escaped with two backslashes (that's what worked for me at least) and the only other escaped character is ]. If you also wanna allow single quotes, just add one after the double quote. Quote Link to comment https://forums.phpfreaks.com/topic/121568-preg_match-validation/#findComment-627154 Share on other sites More sharing options...
thebadbad Posted August 27, 2008 Share Posted August 27, 2008 Of course you don't want to remove the linebreaks from the original string, just do it for the string to test. E.g. use this instead: <?php $string = str_replace("\n", '', str_replace("\r", '', $_POST['message'])); if (!preg_match('~^[-0-9a-z#=|/€$£"&;_,?.@)%+*^[\]>\\\<]+$~iD', $string)) { //error } else { //$_POST['message'] is OK, process it further } ?> Quote Link to comment https://forums.phpfreaks.com/topic/121568-preg_match-validation/#findComment-627166 Share on other sites More sharing options...
daydreamer Posted August 27, 2008 Author Share Posted August 27, 2008 nrg_alpha, i was talking about the first one you gave me, tryd them both but both give errors when i type £ or €, which they should be allowing! Oh well, its not too important i have the majority of the symbols which are useful. Thanks. thebadbad, thanks, but i found this somewhere and it works pretty well: <?php function clean($str){//Removes leading and trailing white space and removes any multiple white space in the source string return preg_replace('/(\s\s+)/', ' ', trim($str)); } ?> but your example still wont except £ or €! Quote Link to comment https://forums.phpfreaks.com/topic/121568-preg_match-validation/#findComment-627167 Share on other sites More sharing options...
thebadbad Posted August 27, 2008 Share Posted August 27, 2008 My code does accept € and £ - you must have some problems with the character encoding. Be sure that the PHP file and the actual HTML handling the form use the same character encoding (UTF-8 is recommended). If you want to allow spaces, add \s inside the character class (square brackets), after the underscore for example. Quote Link to comment https://forums.phpfreaks.com/topic/121568-preg_match-validation/#findComment-627181 Share on other sites More sharing options...
ibechane Posted August 27, 2008 Share Posted August 27, 2008 For special characters, you might consider the unicode reprentation \uXXXX, where XXXX is a 4-digit number (with filler-zeros if necessary) that represents the unicode character. Here is a chart of the codes: http://www.geocities.com/Athens/Academy/4038/graph/fontset.htm So, for instance, the code for the pound symbol is 163, so try replacing the pound symbol with \u0163 in your regex string. Quote Link to comment https://forums.phpfreaks.com/topic/121568-preg_match-validation/#findComment-627189 Share on other sites More sharing options...
daydreamer Posted August 27, 2008 Author Share Posted August 27, 2008 ok thanks. Quote Link to comment https://forums.phpfreaks.com/topic/121568-preg_match-validation/#findComment-627248 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.