Regular Expressions

tooltiperror

Super Moderator
Reaction score
231
So, I want to highlight PHP variables.

The idea would be to take a string of PHP code and use preg_replace to search for a regular expression, and then replace it with itself along with the desired color codes.

I know the regular expression should begin by finding $ and then parse each alphanumeric character or underscore, then stop when it meets a different character or space (since all PHP variables follow that format)

How would I do this in code?
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
\$\w+

That's it... if you like percentages to wrap: "%\$\w+%i", slashes: "/\$\w+/i", etc...
Make sure to use capturing groups accordingly


EDIT: You do know php variables can't start with numbers, right?

Also: Google is your friend -.-
http://www.php.net/manual/en/language.variables.basics.php said:
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
 

tooltiperror

Super Moderator
Reaction score
231
Ugh, when I searched for PHP variable naming rules, this came up first.

Thanks for the help.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top