Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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".

like image 218
homework Avatar asked Sep 06 '09 21:09

homework


2 Answers

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."

like image 163
ysth Avatar answered Sep 18 '22 12:09

ysth


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

i option: allow capital letters or hyphen.

like image 21
Vincent Avatar answered Sep 21 '22 12:09

Vincent