I need to get h2 and h3 tags like $ var in php from this html code below:
<div class="main-info">
<img class="iphone-img" alt="" src="https://www.myweb.com/securedImage.jsp">
<div class="sub-info">
<h2 class="model">iPhone 4S</h2>
<h3 class="capacity color">16GB Black</h3>
</div>
</div>
And I want to get the following results:
echo $model; // Should echo: 'iPhone 4S'
echo $capacitycolour; // Should echo: '16GB Black'
I tried with preg_match, preg_match_alland getElementsByTagName, but so far no luck.
Here is the code I tried:
$pattern = '/[^\n]h2*[^\n]*/';
preg_match_all($pattern,$data, $matches, PREG_OFFSET_CAPTURE);
var_dump($matches);
and
$doc = new DOMDocument();
$doc->loadHTML($data);
$tags = $doc->getElementsByTagName('sub-info');
$root = $doc->documentElement;
foreach($root->childNodes as $node){
$attributes[$node->nodeName] = $node->nodeValue;
}
var_dump($attributes);
source
share