PHP Help with regular expressions... error: Delimiter must not be alphanumeric
I am new to regexp in php. I am just trying to do a simple preg_match on two strings. Here is my code:
$pattern = '\\w*'.strtolower($CK);
if(preg_match($pattern, $rowName, $matches) == true)
{
echo 'true';
}
But I keep getting the error:
Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in /home/daskon/public_html/reflexInvestor_dev/php/rss_functions.php on line 319
I imagine it's because I need to put something after the pattern, but I can;t seme to find what that is. When I try the same pattern in a regexp tester, it works fine.开发者_开发知识库
You need to wrap the pattern in a delimiter that marks the beginning and end. Any "non-alphanumeric, non-backslash, non-whitespace" character works. /
is very common, so:
$pattern = '/\\w*' . strtolower($CK) . '/';
The reason for the delimiter is you can include pattern modifiers in the pattern, but they appear after the delimiter
You need to wrap the pattern in delimiting characters (I like to use #
since I rarely need them inside of the regex):
if (preg_match('#' . $pattern . '#', $rowName, $matches)) {
(You also don't need the == true
, it's redundant since that's what the if does anyway internally...)
精彩评论