0

I want to extract data which is in following form:

<div class="image"><a href="[Any Chacter]">  

I got the data up to <div class="image"> but after that no result. This is my code:

$tag_regex='/<div class="image">/';
preg_match_all($tag_regex,$xml,$matches);

return $matches[0];
Intekhab Khan
  • 1,775
  • 4
  • 18
  • 28
  • 1
    Welcome to Stack Overflow! Please refrain from parsing HTML with RegEx as it will [drive you insane](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). Use an [HTML parser](http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php) instead. – Madara's Ghost May 03 '12 at 16:17
  • Tim cooper :-Thanks i dont know any thing about HTml parsing can do extraction..But for learniing process seems interesting. :) But for the completion of my practical i need it through REgex :( – Intekhab Khan May 03 '12 at 16:44

2 Answers2

1

Just as Truth said in his comment, the proper way to extract data from html is an html parser.

However, your case is simple and could be solved easily and quickly with a regex:

$tag_regex= '<div class="image"><a href=".*">';
preg_match_all($tag_regex,$xml,$matches);

return $matches[0];
Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
0

I'm glad you're open to learning, and I really hope you would learn to use an HTML parser (like any sane human being).

For the practical solution of your problem:

$tag_regex= '|<div class="image"><a href="(.*)">|i';
preg_match_all($tag_regex,$xml,$matches);

return $matches[1]; //Will match what's in the first set of brackets, I.e the href.

Note that this pattern is not robust. It does not account for spaces, different kinds of quotes, newlines, and plenty other things. An HTML parser would account for them all.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • :-I searched all the parsers..i found that Simple HTML DOM Parser is easy to use..But i am not able to download..The download link is not opening ..Is there is any other Parser like Simple HTML DOM Parser.. Or there is other link for download :) – Intekhab Khan May 03 '12 at 17:29