<div class="begin">...</div>
How to match the html inside(including) <div class="begin">
in PHP?
I need a regex solution that can handle nested case.
<div class="begin">...</div>
How to match the html inside(including) <div class="begin">
in PHP?
I need a regex solution that can handle nested case.
Please don't use regular expressions to parse HTML. See http://www.codinghorror.com/blog/archives/001311.html and especially http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454.
This sums it up pretty good.
In short, don't use regular expressions to parse HTML. Instead, look at the DOM classes and especially DOMDocument::loadHTML
// Create DOM from URL
$html = file_get_html('http://example.org/');
echo $html->find('div.begin', 0)->outertext;
here's one way using string methods
$str= <<<A
blah
<div class="begin">
blah blah
blah
blah blah </div>
blah
A;
$s = explode("</div>",$str);
foreach($s as $k=>$v){
$m=strpos($v,'<div class="begin">');
if($m !==FALSE){
echo substr("$v" ,$m);
}
}
output
$ php test.php
<div class="begin">
blah blah
blah
blah blah
Use DOM and DOMXPath instead of regex, you'll thank me for it:
// something useful:
function dumpDomNode ($node) {
$temp = new DOMDocument();
$temp->appendChild($node,true);
return $temp->saveHTML();
}
$dom = new DOMDocument();
$dom->loadHTML($html_string);
$xpath-> new DOMXpath($dom);
$elements = $xpath->query("*/div/[@class='begin']");
foreach ($elements as $el) {
echo dumpDomNode($el); // <-- or do something more useful with it
}
Trying this with regex will lead you down the path to insanity...
Here is your Regex:
preg_match('/<div class=\"begin\">.*<\/div>/simU', $string, $matches);
But: