How do I use a variable pattern with preg_match?


I don't know if this is enough data to feed off of, but I have

preg_match('/SAMPLETEXT/', $bcurl, $cookie1);

and I was wondering if I can make it

preg_match($newfunction, $bcurl, $cookie1);

but when I do, I get this error "Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in".

How can I make it check for my new function, rather than have it check just for "SAMPLETEXT".

9/6/2009 9:33:47 PM

Accepted Answer

Try preg_match("/$newfunction/", $bcurl, $cookie1); so that you are providing the required delimiters (using a delimiter that isn't going to be in $newfunction).

But note that the documentation says "Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster."

9/6/2009 9:36:50 PM

preg_match('/'.$pattern.'/i', $subject)

i option: allow capital letters or hyphen.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow