Skip to content
Advertisement

Validate class/method names with regex

I’m currently working on an MVC Style framework for a company and for security reasons I need to make sure that the controller / method that’s passed via the Query String is valid chars to the RFC (which I can’t find).

I need to be able to validate / sanitize class names according to what’s allowed by the PHP interpreter

For Example:

JavaScript

I need some kind of regex that will validate SomEFunk__YClAssName and sanitize it if need be! This is also the same principles as methods.

There is a few things to take into consideration such as

  • Numerics at the start
  • Only underscores allowed
  • Certain PHP Special Chars to be allowed.

Any information on this or possible expressions would be really helpful.

Here is some of my Router Code so you can see where I need to implement it:

JavaScript

You will see the comment “Sanitize” where the check is needed.

Advertisement

Answer

I believe the regex you’re looking for short class name, e.g. SomeClass is:

JavaScript

According to: http://php.net/manual/en/language.oop5.basic.php


For namespaced class, e.g. AppSomeClass it is:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement