1

I have some values fetched from a TextField, stored to a Array and I'm going to filter it through a for loop. I want to keep out symbols like []{}; and so on, while also allowing whitespaces and the - for some words. So, I constructed this:

$Regex = '/^[a-zA-ZÀ-ÖØ-öø-ÿ0-9 \-]*$/';
$FieldRegex = (preg_match($Regex, $Field[$counter]));

for ($counter = 0; $counter <= 6; $counter++){

if(!$FieldRegex) {

                echo '<script language="javascript">';
                echo 'alert("Only numbers, letters and - are allowed!")';
                echo '</script>';

                header("Location:{$_SERVER['HTTP_REFERER']}");
                break;
                exit;

} else {
                if($counter == 6){
                $Proceed1 = true;
}

The problem that persists through each try is that anything that I try to submit, it won't give me any message and doesn't save to the DB, even if it fits within the regex parameters.

I've been wrapping my head around it but can't seem to find the answer.

What am I doing wrong?

5
  • Try using the /u flag. A special option is the /u which turns on the Unicode matching mode, instead of the default 8-bit matching mode. Commented Feb 5, 2018 at 9:25
  • 1
    Also, move the $FieldRegex = (preg_match($Regex, $Field[$counter])); inside the loop. If you don't, $counter will be undefined. Commented Feb 5, 2018 at 9:26
  • @JustCarty added the u flag like you mentioned as follows: '/^[a-zA-ZÀ-ÖØ-öø-ÿ0-9 \-]$+/u' but it still doesn't work. Doesn't return any input at all. Commented Feb 5, 2018 at 9:40
  • 1
    See ideone.com/bxhJwm. Dean's suggestion is crucial here, I believe. Commented Feb 5, 2018 at 9:41
  • For those characters like Ø you need to specify code points like: "/\x{0080}/u" (note double quotes). PHP strings will not correctly handle this use case. Commented Feb 5, 2018 at 9:44

1 Answer 1

2

You should use a u modifier with a pattern containing Unicode characters for it work correctly in all cases, and you also need to place the $counter setting code line inside the loop for the variable to get assigned a value correctly:

$Regex = '/^[a-zA-ZÀ-ÖØ-öø-ÿ0-9 -]*$/u';
for ($counter = 0; $counter <= 6; $counter++){
    if(!preg_match($Regex, $Field[$counter])) {
                echo '<script language="javascript">';
                echo 'alert("Only numbers, letters and - are allowed!")';
                echo "</script>\n";
                header("Location:{$_SERVER['HTTP_REFERER']}");
                break;
                exit;

    } else {
                if($counter == 6){
                    $Proceed1 = true;
                }
    }
}

See the PHP demo.

Note that in case you need to match any letters, you may change the '/^[a-zA-ZÀ-ÖØ-öø-ÿ0-9 -]*$/u'; regex to '/^[\p{L}0-9 -]*$/u'; where \p{L} matches any Unicode letter.

Sign up to request clarification or add additional context in comments.

1 Comment

I've added your Unicode Regex to the mix and for a while it worked but did not yield any alerts whatsoever. Now, for some reason, it's throwing off most of my code and causing the whole saving system to fail. I have no clue why. I'll give it a try on an online host, to see if the problem is only local. I'll mark yours as correct once I test it properly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.