I am building an app (happens to be PHP) that needs to build regex patterns on the fly based upon user input. Of course, since it’s user input, the values may containn all manners of characters that have special meanings in regex patterns, and we want them to hold their special meanings in this case. (example user input might be “over (9” or “here]” or anything.
I am certain that I ran across a PHP builtin function that will prepare/clean up such regex, but for the life of me I cannot find it. I could of course write my own function to perform this task, but for performance reasons (and due to some reluctance to reinvent anything) I would prefer the built-in. As I recall, that function also had an optional parameter where you could specify the delimeter (and it too would be replaced).
Does such a builtin acutally exist, and if so, what is its name? Google searches for this like “regular expression cleanup” and “regular expression builder” and so on have not resulted in what I want. I have pored through the “string functions” more than once in the manual (php 8.2) and not found anything, but there’s so much there I might have glanced over it.
By the way, this is my approximate way to handle it manually:
$regExp = '%' . preg_replace('/[%.+*?^$()[]{}|]/', '\1', $userInput) . '%' ;
Thank you!