Page 1 of 1

preg_match() syntax issue

Posted: Mon Nov 24, 2008 9:20 am
by bo0sted
Hello,

I'm trying to use preg_match() to locate the follow string which is defined as $match_expression.

Code: Select all

   
    $total = file_get_contents('http://www.mywebsite.com'); 
    $match_expression = '<td><a href="/(.*)\">'; 
    preg_match($match_expression,$total,$matches);
 
When running the page I get the follow error: Warning: preg_match() [function.preg-match]: Unknown modifier '<'.

I need to know how to modify $match_expression to have preg_match() find what i'm looking for, but i'm confused on how I need to change the syntax.

Any help is greatly appreciated!

Thanks!

Re: preg_match() syntax issue

Posted: Tue Nov 25, 2008 11:05 am
by mintedjo
preg_match requires that regular expressions are enclosed in forward slashes
Because you havea forward slash in your regex you need to escape it with a backslash

Code: Select all

$match_expression = '/<td><a href="\/(.*)\">/';
That should work

EDIT: I just noticed you have one escaped double quote in there and another that isn't escaped
You shouldn't need to escape any of them because your'er using single quotes to define the string.