Forum Moderators: coopster

Message Too Old, No Replies

variable inside a regular expression

         

Anguz

11:01 pm on Sep 10, 2003 (gmt 0)

10+ Year Member


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)

10+ Year Member



Try this ...

$regex = "<match>".$variable."<more stuff to match>";
ereg($regex, $string, $regs);

vincevincevince

9:35 am on Sep 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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)

WebmasterWorld Senior Member 10+ Year Member



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)

10+ Year Member



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)

WebmasterWorld Administrator 10+ Year Member



yes you do
"=~" == perl

I am sure that is an oops ;)

Anguz

5:37 pm on Sep 11, 2003 (gmt 0)

10+ Year Member



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)

10+ Year Member



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.

daisho.