how do I use a variable inside a regular expression in php?
thank you in advance
liketoseeyoutry
12:55 am on Sep 11, 2003 (gmt 0)
Try this ...
$regex = "<match>".$variable."<more stuff to match>"; ereg($regex, $string, $regs);
vincevincevince
9:35 am on Sep 11, 2003 (gmt 0)
There is no problem with using variables within regular expressions. Either conc them together with . i.e. "/reg-".$var."-exp/" or just include them straight away "/reg-$var-exp/"
MonkeeSage
10:07 am on Sep 11, 2003 (gmt 0)
my $blah = "33"; my $foo = "blah33blah44"; if ($foo =~ s/\Q$blah/22/g) { print $foo; }
Jordan
daisho
5:25 pm on Sep 11, 2003 (gmt 0)
MonkeeSage does that work in PHP? Since when? That looks like perl to me. I though you had to use preg_* functions in PHP for regex?
daisho.
jatar_k
5:30 pm on Sep 11, 2003 (gmt 0)
yes you do "=~" == perl
I am sure that is an oops ;)
Anguz
5:37 pm on Sep 11, 2003 (gmt 0)
I see
I have seen =~ in a php script once before, what does that mean?
daisho
3:55 pm on Sep 12, 2003 (gmt 0)
Unlikely Anguz unless it was in quotes which meant it didn't mean anything to PHP.
A quick look at the PHP Parser Tokens [php.net] shows that tilde "~" is not one.