Forum Moderators: coopster

Message Too Old, No Replies

£ sign causing confusion in preg match statement

         

surrealillusions

9:49 pm on Feb 17, 2009 (gmt 0)

10+ Year Member



Hi,

I'm currently using the following lines to check a comments field for valid input (amongst other fields). I've just cut it down to the comments field for simplicity and left out the rest of the script that deals with sending an email. As it is, with the £ sign in there, it basically doesnt like it. When you type a message and include the £ sign, it says its invalid message when you try to submit the form. Leave the field blank, and it accepts it, even though the minimum number of characters should be 3.

I have tried getting round it by replacing the £ sign with -- for example but with no success. I have no idea why it wont accept the £ sign. The file is in utf-8 encoding if thats any help.

// checks for compuslory fields
$formName = array('comments');
$errornum = 0;

if (isset($_POST['submitform'])) {
foreach ($formName as $value) {
if (!(isset($_POST[$value]))) {
$_POST[$value] = "";
}
$errorArray[$value] = "";

// Test for comments
if (addslashes(trim($value == "comments")) and (!preg_match('/^([a-zA-Z0-9\s\\\£\.\,\!\&\?\-\"\'\;\_\:\@\~\^\%\$\*\(\)\=]{3,400})$/',$_POST[$value]))) {
$errorArray[$value] = "Invalid message";
$errornum++;
}

henry0

11:12 pm on Feb 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you do not need addslashes
if you are using mysql_real_escape_string
look at my example

A) run it as is
B) remove addslashes, it reports my curly braces intrusion
C) remove the curly braces and it works

at least it worked for me!

<<<
<?php
$test=addslashes(" asas asas {{}{ £");
// Test for comments
if( !preg_match('/^([a-zA-Z0-9\s\\\£\.\,\!\&\?\-\"\'\;\_\:\@\~\^\%\$\*\(\)\=]{3,400})$/',$test)) {
echo"my bad";
}
?>
>>>

surrealillusions

11:54 pm on Feb 17, 2009 (gmt 0)

10+ Year Member



Nope..not quite.

Its solved the one problem but it wont submit the form. Says theres errors on the form, when all fields are valid.

$comments = addslashes($_POST['comments']);

if( !preg_match('/^([a-zA-Z0-9\s\\\£\.\,\!\&\?\-\"\'\;\_\:\@\~\^\%\$\*\(\)\=]{3,400})$/',$comments)) {
$errorArray[$comments] = "Invalid message";
$errornum++;
}

The $errornum++ is still been done, when its not supposed to be. When it encounters an error, and displays the error message, it stops the form been sent. If no errors are displayed (thus no errornum++ is been carried out and so equals 0), then the form us submitted. When i take that one line out, it works fine.