0

I'm using preg_match() to check if a user inputted string matches a required format. Previously, the local version of the website returned the same value as the production version, however, now the local version is returning False when it should be True.

Here's a sample of the code I'm using to validate a user's first name:

$firstName = 'John';
if (preg_match("/^[A-Z\p{L}][A-Z\' -\p{L}]{0,49}$/ui", $firstName)) {
echo 'True';
} else {
echo 'False';
}

The local version incorrectly returns False. In case it's relevant, it's running PHP version 5.5.17.

The production version correctly returns True. In case it's relevant, it's running PHP version 5.3.29.

I don't see any recent changes in the PHP spec for preg_match. Can anyone tell me why these 2 versions of the website return differing values when using the same code? Thank you.

Ed Brissenden
  • 177
  • 2
  • 13
  • Using v5.6.3 I get an error compiling the regex: invalid range in character class at offset 20 – Tom Fenech Mar 05 '15 at 14:14
  • I got the same result as @TomFenech – Funk Forty Niner Mar 05 '15 at 14:17
  • 1
    These `-\p` seem to be the offending characters. You can do `-\'p` which will return TRUE. You may have forgotten a `'` before the `p` - Or try `[A-Z\-\p{L}]` – Funk Forty Niner Mar 05 '15 at 14:20
  • Related: http://stackoverflow.com/q/888838/2088135 – Tom Fenech Mar 05 '15 at 14:21
  • So, amidst the comments and an answer given; is this question still open? – Funk Forty Niner Mar 05 '15 at 14:53
  • Thank you for your comments @TomFenech and Fred-li , it's interesting that it doesn't compile, yet it still works on the production site, and used to work on the dev site. I guess something changed at some point. Fred, in your last example, what you suggested I try doesn't include the necessary space and apostrophe, which I want to continue to allow. I think the second part of Adam's answer looks like what I need, and once I have finished testing it I will comment and accept it as the correct answer (assuming all is ok). Thanks again. – Ed Brissenden Mar 05 '15 at 15:26
  • FYI: `[A-Z]` is included in `\pL`, so your regex can be simply: `\p{L}[\p{L}\' -]{0,49}` – Toto Mar 05 '15 at 16:40
  • Thanks @JESUISCHARLIE - good catch. It will still need the hyphen escaped, as per Adam T's suggested fix below, like so: [\p{L}\' \-]{0,49} – Ed Brissenden Mar 05 '15 at 19:18
  • @Edward: The hyphen doesn't need to be escaped if it is in first or last position in a character class. – Toto Mar 06 '15 at 09:01

1 Answers1

0

I also get the same result as @TomFenech, however if I remove the - before the 2nd "p". "/^[A-Z\p{L}][A-Z\' \p{L}]{0,49}$/ui", $firstName it responds with True

$firstName = 'John';
if (preg_match("/^[A-Z\p{L}][A-Z\' \p{L}]{0,49}$/ui", $firstName)) {
    echo 'True';
} else {
    echo 'False';
}

However, I also escaped the dash and also got True.

if (preg_match("/^[A-Z\p{L}][A-Z\' \-\p{L}]{0,49}$/ui", $firstName)) {

$ php -v
PHP 5.5.19 (cli) (built: Nov 14 2014 01:09:01)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
Adam T
  • 675
  • 8
  • 22
  • Thank you Adam, I think the second part of your answer looks like the solution I need, as it retains the existing regex format but with the desired validation outcome. I will test this and get back to you shortly. – Ed Brissenden Mar 05 '15 at 15:29
  • Cool. Hope it helped :) – Adam T Mar 05 '15 at 17:42
  • It did, I've since tested this and this solution resolves the issue exactly as hoped, thanks again! – Ed Brissenden Mar 05 '15 at 19:19