preg_match() syntax issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bo0sted
Forum Newbie
Posts: 1
Joined: Mon Nov 24, 2008 9:13 am

preg_match() syntax issue

Post 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!
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: preg_match() syntax issue

Post 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.
Post Reply