user993683
user993683

Reputation:

preg_match_all() html tags

Using preg_match_all(), I want to match something like:

...randomtext...>MATCH1</a>" (MATCH2)"...randomtext... EDIT: to clarify, this is exactly the string I'm trying to extract data from, including the brackets, quotes, angle-brackets etc.

Here's what I've tried: preg_match_all("/^>(.+?)</a>\" \((.+?)\)\"$/", $htmlfile, $matches);

It should extract MATCH1 as $matches[1][0] and MATCH2 as $matches[2][0]

Any idea why it isn't working?

Thanks

Upvotes: 1

Views: 3620

Answers (2)

bozdoz
bozdoz

Reputation: 12860

You didn't escape your end tag </a>

This should work:

preg_match_all("/>(.+?)<\/a>\" \((.*?)\)/", $htmlfile, $matches);

See Codepad example.

Upvotes: 3

smathy
smathy

Reputation: 27961

You need to escape the / in your pattern, and you don't want your pattern anchored to ^ and $

So probably this will work: preg_match_all("/>(.+?)<\/a>\" \((.+?)\)\"/", $htmlfile, $matches);

Upvotes: 3

Related Questions