Valid PHP Function Name RegEx

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
david64
Forum Commoner
Posts: 53
Joined: Sat May 02, 2009 8:12 am
Location: Wales

Valid PHP Function Name RegEx

Post by david64 »

Hi,

The regex that sets out the boundaries for valid function names in PHP is:

Code: Select all

[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
Does anyone know if there is a quicker to test something against this pattern than preg_match?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Valid PHP Function Name RegEx

Post by requinix »

Perhaps function_exists? That's for functions that already exist of course; for callbacks you should use is_callable.

If you want to check for a possible function name then there's a problem: PHP actually allows just about anything. It's just that the

Code: Select all

function foo
syntax restricts you and you can't really create a function without using that. For example, lambda functions have a null character at the end.

But anyways, there isn't really a better method. How are you using it?
Post Reply