...

View Full Version : Regular Expression Question



zach4618
08-02-2008, 01:41 AM
I am having a problem with validation for a form which uses regular expressions for validation purposes. I would like to allow line breaks in a text area field, but haven't had any luck so far. Any advice is appreciated. Thanks.

oesxyl
08-02-2008, 02:12 AM
I am having a problem with validation for a form which uses regular expressions for validation purposes. I would like to allow line breaks in a text area field, but haven't had any luck so far. Any advice is appreciated. Thanks.
I guess you can post the relevant part of the code.

regards

zach4618
08-02-2008, 05:26 AM
So here is the regular expression:


if (!eregi("^([[:alnum:]]|-| |\.|,|\(|\)|\?|')+$", $message)) {$messageError=1; $error=1;}

What I would like to do is add line breaks to the allowed characters. IE:

"Hello,
Here is my message"

This would currently return an error in the form. I would like to add to the regular expression to address that.

oesxyl
08-02-2008, 05:34 PM
So here is the regular expression:


if (!eregi("^([[:alnum:]]|-| |\.|,|\(|\)|\?|')+$", $message)) {$messageError=1; $error=1;}

What I would like to do is add line breaks to the allowed characters. IE:

"Hello,
Here is my message"

This would currently return an error in the form. I would like to add to the regular expression to address that.
in php manual is a warning regarding ereg....:


These regular expression functions are not binary-safe. The PCRE functions are.


try this:


if(!preg_match("/^([\w\s.,?()-]+)$/m",$message)){
print "your error message or code here";
}


regards

zach4618
08-02-2008, 07:59 PM
Thanks for the post - that solved my initial problem. However a new one was created. Later in the form I use a header function to redirect to a new page:


header ('Location: success.php?message=' . $message . '');

If I replace the eregi function like you suggested, I get the following error message when I try and submit the form:

Warning: Header may not contain more than a single header, new line detected. in /home/.usual/artifexproductions/artifexproductions/contact.php on line 51

If I revert to the eregi function this error goes away.

oesxyl
08-02-2008, 08:20 PM
Thanks for the post - that solved my initial problem. However a new one was created. Later in the form I use a header function to redirect to a new page:


header ('Location: success.php?message=' . $message . '');

If I replace the eregi function like you suggested, I get the following error message when I try and submit the form:

Warning: Header may not contain more than a single header, new line detected. in /home/.usual/artifexproductions/artifexproductions/contact.php on line 51

If I revert to the eregi function this error goes away.
wrong conclusion, wrong solution!
replacing something that don't work with something that work has changed the logic. You just discover that you have more then one problem in your script.

regards

Fou-Lu
08-02-2008, 08:52 PM
$message from a preg_match or preg_match_all will be an array not a string. You will likely want to use a $message[0] for you're result, but its hard to say for certain. Dump out the contents of $message and use it to determine what you need to display.

oesxyl
08-02-2008, 09:32 PM
$message from a preg_match or preg_match_all will be an array not a string. You will likely want to use a $message[0] for you're result, but its hard to say for certain. Dump out the contents of $message and use it to determine what you need to display.
I miss something? :)
preg_match here have only two arguments, that means first is the pattern, second the subject and it return false or true if it match or not. Just test a condition, :)
I have no idea how preg_match_all can be used here, is a validation only.
second problem is because the logic was changed.

PS: I expect from OP to post the code if need help.

regards

Fou-Lu
08-02-2008, 10:28 PM
I miss something? :)
preg_match here have only two arguments, that means first is the pattern, second the subject and it return false or true if it match or not. Just test a condition, :)
I have no idea how preg_match_all can be used here, is a validation only.
second problem is because the logic was changed.

PS: I expect from OP to post the code if need help.

regards

Nope, my bad, I was thinking on matches which doesn't exist here (only two args). Thats what I get for trying to help when I first wake up ;)
In that case, try urlencoding and trimming you're message before putting it in the header.

oesxyl
08-02-2008, 10:45 PM
Nope, my bad, I was thinking on matches which doesn't exist here (only two args). Thats what I get for trying to help when I first wake up ;)
anybody have right to be wrong, including as, :)


So here is the regular expression:


if (!eregi("^([[:alnum:]]|-| |\.|,|\(|\)|\?|')+$", $message)) {$messageError=1; $error=1;}

What I would like to do is add line breaks to the allowed characters. IE:

"Hello,
Here is my message"

This would currently return an error in the form. I would like to add to the regular expression to address that.


In that case, try urlencoding and trimming you're message before putting it in the header.
I don't make any connection untill you said that. :)
I guess this is the solution.

regards

zach4618
08-02-2008, 10:54 PM
wrong conclusion, wrong solution!
replacing something that don't work with something that work has changed the logic. You just discover that you have more then one problem in your script.

regards

Actually I am replacing something that works with something that doesn't. The form works fine until I add the preg_match function.


In that case, try urlencoding and trimming you're message before putting it in the header.

What function should I use to accomplish this?

oesxyl
08-02-2008, 11:32 PM
Actually I am replacing something that works with something that doesn't. The form works fine until I add the preg_match function.
is my fault. I will avoid to do "something that doesn't" work for you.


What function should I use to accomplish this?

Fou-Lu
08-02-2008, 11:52 PM
The function trim() to remove any trailing or starting whitespace (including \r\n\c characters), and urlencode() / urldecode() for sending and retrieving the message from the $_GET.


header ('Location: success.php?message=' . urlencode(trim($message)) . '');

When displaying the message from the sucess.php page, use urldecode($message) to reformat the string. The problem is that url doesn't like having spaces and a few other characters in it. This allows you to treat it as a value the GET can understand and keep together correctly for later extraction.
This is why I like post. Too bad its really tricky to send it as a header >.<

This of course will only work if the preg_matching is correct.


I'm looking at this pattern as well. A developer who has more experience with patterns may need to confirm, but I don't believe that [:alnum:] is available for use in preg. Instead, using a range: [a-z0-9] with an insensitive flag would provide the same usage:


$pattern = "/^([a-z0-9]|-|\.|,|\(|\)|\?|')+$/mi";

This sould say any character that is {a-zA-Z0-9}-.,()? or ' at least once.
Like I said, I don't know for certain if alnum is unavailable. I'm guessing its a part of POSIX, not PCRE, but I could be wrong.

zach4618
08-03-2008, 06:14 AM
Thank you both for your helpful responses.

Fou-Lu
08-03-2008, 06:52 AM
NP.
I'm guessing you've got it figured out :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum