PHP and Regexp to Accept Only Greek Characters in Form

PHP and regexp to accept only Greek characters in form

I'm not too current on the Greek alphabet, but if you wanted to do this with the Roman alphabet, you would do this:

/^[a-zA-Z\s]*$/

So to do this with Greek, you replace a and z with the first and last letters of the Greek alphabet. If I remember right, those are α and ω. So the code would be:

/^[α-ωΑ-Ω\s]*$/

Regular expression to accept only greek characters

You may match chars with \p{Greek} and you must use the /u modifier:

'~^\p{Greek}{2,3}[0-9]{3,4}$~u'

See the regex demo.

Pattern details

  • ^ - start of string
  • \p{Greek}{2,3} - 2 or 3 Greek chars
  • [0-9]{3,4} - 3 or 4 ASCII digits
  • $ - end of string.

Sample Image

Regular expression testing with Greek characters php

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 $).

Regex - Greek Characters in URL

You can use

$router->get('/theatre/[^/]+', 'TheatreController', 'single_post');

as [^/]+ will match one or more characters other than / since [^...] is a negated character class that matches any char but the one(s) defined in the class.

Note you do not have to use \d if you used \w (\w already matches digits).

Also, you did not match diacritics with your regex. If you need to match diacritics, add \p{M} to the regex: '/theatre/[-\w\p{M}!.]+'.

Note that to allow \w to match Unicode letters/digits, you need to pass /u modifier to the regex: $found = preg_match("#^$value$#u", $path). This will both treat input strings as Unicode strings, and make shorthand patterns like \w Unicode aware.

Another thing: you need not escape . inside a character class.

Pattern details:

  • #...# - regex delimiters
  • ^ - start of string
  • $value - the $value variable contents (since double quoted strings in PHP allow interpolation)
  • $ - end of string
  • #u - the modifier enabling PCRE_UTF and PCRE_UCP options. See more info about them here

Regex to allow any language characters in the form of full name and starting with letter

You could do this through the use of Unicode Categories. Thus, the regular expression ^\p{Lu}\p{Ll}+( \p{Lu}\p{Ll}+)*$ will match an upper case letter followed by one or more lower case letters, followed by 0 or more other names, seperated by spaces. An example of the expression is shown here.

With regards to your second point, you could use something of the sort ^\p{Lu}\p{Ll}*$, which will expect at least 1 upper case letter followed by 0 or more lower case ones.

The expressions above assume that you do not have quotation marks, such as O'Brian or dashes Foo-bar in your names. If you want to handle specifically Greek names, and you know for a fact that Greek names have neither quotation marks nor dashes in them, then this should not be much of a problem.

Usually one simply ensures that the name provided is not empty, rather than specifying some strict form. Please refer to this question for more information on the matter.

PHP: Check for characters in the Latin script plus spaces and numbers

There are at least two things to change:

  • Add u flag to support chars other than ASCII (/^\p{Latin}+$/ => /^[\p{Latin}]+$/u)
  • Create a character class for letters, digits and whitespace patterns (/^\p{Latin}+$/u => ^[\p{Latin}]+$/u)
  • Then add the digit and whitespace patterns. If you need to support any Unicode digits, add \d. If you need to support only ASCII digits, add 0-9.

Thus, you can use

preg_match('/^[\p{Latin}\s0-9]+$/u', $testString) // ASCII only digits
preg_match('/^[\p{Latin}\s\d]+$/u', $testString) // Any digits

Also, \s with u flag will match any Unicode whitespace chars.



Related Topics



Leave a reply



Submit