I have a regular expression that is used to re-define a constant in php file using preg_match and input file is screened using htmlspecialchars
eg for
define('MEMBERSHIP', 'GOLD');
the following regex works
/define.*["e\']' . $constant . '["e;\'].*;/i
however it matches the last semi colon, works in most scenarios but fails in a case like the following
eg:
define("MEMBERSHIP", 'GOLD'); // membership subscription; empty means not in use.
notice the last semicolon, resulting in replaced code as
define("MEMBERSHIP", 'SILVER'); empty means not in use.
which breaks the code. tried the regex below but it didn’t work for those with double quote
/define.*["e;\']' . $constant . '["e;\'][^;]*;/i
any idea how to fix this?
You can match corresponding quotes by using a backreference:
Otherwise, the negated character class you have at the end is definitely the way to go.