JMC
JMC

Reputation: 1697

Regex: Match html tag only if it contains a specific class id

Match an html tag using perl regex in php.

Want the tag to match if it contains "class=details" somewhere in the open tag.

Wanting to match <table border="0" class="details"> not <table border="0">

Wrote this to match it:

'#<table(.+?)class="details"(.+?)>#is'

The <table(.+?) creates a problem since it matches the first table tag it finds only stopping the match when it finds class="details" no matter how far down the code it occurs.

I think this logic would fix my problem:

"Match <table but only if it contains class="details" before the next >"

How can I write this?

Upvotes: 1

Views: 10455

Answers (4)

PandaDev
PandaDev

Reputation: 1

You could possibly use a Regex like the following:

<\/?table[^>]*(class="details")*>

But the above users are correct in saying that it would be much better to use a xml/html type parser to find your item.

Upvotes: 0

Andrew Moore
Andrew Moore

Reputation: 95334

While regular expressions can be good for a large variety of tasks, I find it usually falls short when parsing HTML DOM. The problem with HTML is that the structure of your document is so variable that it is hard to accurately (and by accurately I mean 100% success rate with no false positive) extract a tag.

What I recommend you do is use a DOM parser such as phpQuery and use it as such:

function get_first_image($html){
    $dom = phpQuery::newDocument($html);

    $first_img = $dom->find('img:first');

    if($first_img !== null) {
        return $first_img->attr('src');
    }

    return null;
}

Some may think this is overkill, but in the end, it will be easier to maintain and also allows for more extensibility. For example, using the DOM parser, I can also get the alt attribute.

A regular expression could be devised to achieve the same goal but would be limited in such way that it would force the alt attribute to be after the src or the opposite, and to overcome this limitation would add more complexity to the regular expression.

Also, consider the following. To properly match an <img> tag using regular expressions and to get only the src attribute (captured in group 2), you need the following regular expression:

<\s*?img\s+[^>]*?\s*src\s*=\s*(["'])((\\?+.)*?)\1[^>]*?>

And then again, the above can fail if:

  • The attribute or tag name is in capital and the i modifier is not used.
  • Quotes are not used around the src attribute.
  • Another attribute then src uses the > character somewhere in their value.
  • Some other reason I have not foreseen.

So again, simply don't use regular expressions to parse a dom document.

Simple example on how to solve your problem with phpQuery:

$dom = phpQuery::newDocument($html);
$matching_tags = $dom->find('.details');

Upvotes: 4

Gaim
Gaim

Reputation: 6844

HTML is not parseable ( reliably ) using regular expressions. There are few simple cases which have a solution but they are exceptions. I think that your case is unsolvable using regex but I am not sure

You should work with it using XML tools and XML parsers like XPath for searching and testing your conditions. There is very simple to write the expression which matches your case. I don't know how to build XML tree and execute XPath query in PHP but XPath expression is

//table[@class='details']

Upvotes: 1

Scuzzy
Scuzzy

Reputation: 12332

You will probably need a Positive Look Ahead of some form, as a very crude one that clearly has its limitations...

<table(?=[^>]*class="details")[^>]*>

Upvotes: 1

Related Questions