Can I store and call regex in variables for later use?
I plan on storing regular expression codes in a database, but not sure how to get t开发者_高级运维hem from a variable to function.
Any advise?
$i = "([wx])([yz])"
$j = "[^A-Za-z0-9]"
$k= "([A-Z]{3}|[0-9]{4})"
//Wold this execute properly, this really is the extent of my question?
preg_match($i, $string);
Regular expressions are simply strings, so you could store them as such in your database.
It should work, the only thing you're missing are delimiters.
http://php.net/manual/en/regexp.reference.delimiters.php
When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.
Often used delimiters are forward slashes (/), hash signs (#) and tildes (~). The following are all examples of valid delimited patterns.
/foo bar/
#^[^0-9]$#
+php+
%[a-zA-Z0-9_-]%
You could also store the expressions without delimiters and add them later.
精彩评论