How to clear H2 and H3 tags from html page in PHP?

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);
+4
source share
4 answers

sub-info is a class, not a tag, so your use of DOMDocument is wrong, you can use XPath query better.

$strhtml='<div class="main-info">
            <img class="iphone-img" alt="" src="https://www.myweb.com/securedImage.jsp?configcode=DTF9&size=120x120">
            <div class="sub-info">
                <h2 class="model">
                        iPhone 4S
                </h2>
                <h3 class="capacity color">
                    16GB Black 
                </h3>
            </div>
        </div>';


$doc = new DOMDocument();
$doc->loadHTML( $strhtml );
$xpath=new DOMXPath( $doc );
$col=$xpath->query('//div[@class="sub-info"]/h2|//div[@class="sub-info"]/h3');
if( $col ){
    /* You could store results from query in an array */
    $tags=array();
    foreach( $col as $node ) {

        /* Simplest form to display results on separate lines, use br tag */
        echo $node->nodeValue . '<br />';

        /* Add tags to array - a rethink would be required if there are multiple h2 and h3 tags! */
        $tags[ $node->tagName ]=$node->nodeValue;

    }
    /* echo back results from array */
    echo $tags['h2'];
    echo '<br />';
    echo $tags['h3'];
}
+5
source

simple_html_dom.php . CSS . , , , URL-! Element s:

$div = $html->find('div.sub-info');
$ret = $div[0]->find('h2, h3');

API:

. RegEx HTML, , :)

0

- , .

H2- : .*<h2.*>[\n\s]*(.*) ( , )

0

, Cyberboki?

.

$strhtml='<div class="main-info">
        <img class="iphone-img" alt="" src="https://www.myweb.com/securedImage.jsp?configcode=DTF9&size=120x120">
        <div class="sub-info">
            <h2 class="model">
                    iPhone 4S
            </h2>
            <h3 class="capacity color">
                16GB Black 
            </h3>
        </div>
    </div>';
$new = preg_replace("/\s+/",' ',$strhtml);  
preg_match('/<h2 class="model">(.*?)<\/h2>/i', $new , $h2); 
preg_match('/<h3 class="capacity color">(.*?)<\/h3>/i', $new , $h3); 

echo "option 1";
echo "<br/>";
echo $h2[1];
echo "<br/>";
echo $h3[1];
echo "<br/>";
echo "<br/>";

    $ex = explode("\n",strip_tags($strhtml));   
    foreach($ex as $key){
        //echo $key;
        $line_out = preg_replace('/\s+/', ' ', trim($key));
        if(strlen($line_out) > 0){
            $rr[] = trim($key);
        }
    }
echo "option 2";
echo "<br/>";       
echo $rr[0];
echo "<br/>";
echo $rr[1];        

result:
option 1
iPhone 4S
16GB Black

option 2
iPhone 4S
16GB Black 

, iPhoneYeta​​p >

0

Source: https://habr.com/ru/post/1620505/


All Articles