user3209603
user3209603

Reputation: 175

Regular expression testing with Greek characters php

Regular expressions are getting the better of me today. I've been searching google finding code that people say works ... and it doesn't for me!

Background: I want to validate user input into a text field. The input can contain: 1). English characters and numbers or 2). Greek character and numbers If symbols (&%!) etc are found it should not match!

Currently: Been testing this RegEx in PHP

/^[\p{Greek}\s\d a-zA-Z]+/u

testing with "γφψη 677" works testing with "γφψη 677###hello" works too!! - BUT I WANT THIS TO FAIL

<?php
header("content-type: text/html;charset=utf-8");

if (preg_match_all('/^[\p{Greek}\s\d a-zA-Z]+/u', 'γφψη 677###hello')){
    echo "ok!";
}
else {
    echo "no!";
}
?>

Any ideas?

Thank you in advance!!

Upvotes: 1

Views: 1775

Answers (1)

npinti
npinti

Reputation: 52185

The problem with your regex: /^[\p{Greek}\s\d a-zA-Z]+/u is that it tells your engine what to start matching. That being said, it does not provide any instructions on what to do at the end of your string. Changing your regex to this: /^[\p{Greek}\s\d a-zA-Z]+$/u (notice the $ at the end) should fix the problem.

The ^ and $ combo essentially instruct the regex engine to start matching at the beginning of the string (^ and at the end $).

Upvotes: 2

Related Questions