php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #64907 classname regex seems invalid
Submitted: 2013-05-23 06:40 UTC Modified: 2013-05-23 18:26 UTC
From: php at kingsquare dot nl Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: n/a
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php at kingsquare dot nl
New email:
PHP Version: OS:

 

 [2013-05-23 06:40 UTC] php at kingsquare dot nl
Description:
------------
I've tried to use the given regex for testing of my classname (which is 
dynamically built from various sources) but it seems that the given regex allows 
characters which shouldnt be allowed.

The page http://php.net/manual/en/language.oop5.basic.php gives the '^[a-zA-
Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' regex. But this would allow not only strange 
characters like '»×' which might be intened but also 'a:' which ofcourse would 
fail from a parse error... 



Test script:
---------------
<?php
$controllerName = 'a:';

var_dump(preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $controllerName));
eval('class '.$controllerName.' {}');
exit;

Expected result:
----------------
I'd expect the parse error, but also the not the valid pregmatch. thus:

int 0
( ! ) Parse error: syntax error, unexpected ':', expecting '{' in test.php(5) : 
eval()'d code on line 1

Actual result:
--------------
int 1
( ! ) Parse error: syntax error, unexpected ':', expecting '{' in test.php(5) : 
eval()'d code on line 1


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-05-23 07:58 UTC] salathe@php.net
Your testing regular expression is not anchored to the end of the subject string. 
It is matching *only* the letter "a".  Try again with a $ anchor.
 [2013-05-23 08:21 UTC] php at kingsquare dot nl
The regex just matches the first and rest characters. The point is that 'a2:' as 
a class is allowed according to the regex, but isnt by the php engine... The 
colon isnt allowed in a classname. Thus the regex isnt correct. (and should be 
something like ^[a-zA-Z_][a-zA-Z0-9_]*

Thus allowing the following:
class a
class _a
class a3

but disallowing 
class 3
class ~a
class »×
class a:

Which imho are all weird anyway ;)
 [2013-05-23 18:26 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2013-05-23 18:26 UTC] aharvey@php.net
I think you've misunderstood the regex, as salathe suggested. In English, it allows letters, underscores, and bytes from 0x7f to 0xff inclusive as the first character, then the same plus digits as subsequent characters. That's correct, as http://codepad.viper-7.com/PYIXWa demonstrates.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 17:00:02 2026 UTC