Regex to match 2 html tags in 1 HTML file?

Regex does not always provide perfect result while parsing HTML.

Regex does not always provide perfect result while parsing HTML. I think you should do it using HTML DOM Parser For Example: // Create DOM from URL or file $html = file_get_html('example.com/'); // OR Create a DOM object from a HTML file $html = file_get_html('test. Htm'); // Find all images foreach($html->find('img') as $element) echo $element->src .''; // Find all links foreach($html->find('a') as $element) echo $element->href .

''; There are filters to get tags with specific attributes: attribute Matches elements that have the specified attribute. Attribute=value Matches elements that have the specified attribute with a certain value. Attribute!

=value Matches elements that don't have the specified attribute with a certain value. Attribute^=value Matches elements that have the specified attribute and it starts with a certain value. Attribute$=value Matches elements that have the specified attribute and it ends with a certain value.

Attribute*=value Matches elements that have the specified attribute and it contains a certain value. More Options There are also some other Parsing Tools to parse HTML as described in this answer.

Well I need to use regex. I've done it before but I can't remember what I did so I know it's possible... – CyberK Dec 25 '10 at 9:42 1 @CyberK: I have also done it using regex before and I spent lot of time finding out specific regex patterns for different HTML codes. And the result was not always perfect.

Finally I realized to use a HTML Parser. Anyway it is also a solution. May be you will get a good regex.

– NAVEED Dec 25 '10 at 9:45 2 This is definitely the easiest way. I used to parse html with regex too, but the headaches it brings are NEVER worth it! Heed the advice and use a proper html parser – jfoucher Dec 25 '10 at 10:33 @mario: I did not know the history behind this.

Anyway I replaced this with your provided link. – NAVEED Dec 25 '10 at 18:42 Thank you. (And anyway the link isn't forbidden yet.

Just redundant if you have a better answer. ) – mario Dec 25 '10 at 20:36.

Hmmm, I'll better elaborate before more anti-regex memers come around. In your case it is actually applicable to use regular expressions. However I'd like to point out, that you should carefully evaluate on the pros and cons.It's mostly simpler to use phpQuery or QueryPath for such tasks: qp($html)->find("img")->attr("src"); But a regex is possible too, if you don't overlook the gritty details: preg_match('#+src="(^">*)".

+? *image="(^">*)"#ims', $html, $m); If extraction depends on the presence of both tags, then it might be a better option here.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions