简体   繁体   中英

How to extract Tag Value from PHP not Tag code?

I need to get the 4th H4 Tag Value HTML Code looks like below :

                  <div class="col-md-6 mb-6 mb-md-3">
                    <h3 class="mb-3">testing 1</h5>
                    <h4 class="mb-0 abcd defg hikjk lmnop">Not needed this Value</h4>
                  </div>

                  <div class="col-md-6 mb-6 mb-md-3">
                    <h3 class="mb-3">testing 2 </h5>
                    <h4 class="mb-0 abcd defg hikjk lmnop">Not needed this Value</h4>
                  </div>

                  <div class="col-md-6 mb-6 mb-md-3">
                    <h3 class="mb-3">testing 3</h5>
                    <h4 class="mb-0 abcd defg hikjk lmnop">Not needed this Value</h4>
                  </div>

                  <div class="col-md-6 mb-6 mb-md-3">
                    <h3 class="mb-3">testing</h5>
                    <h4 class="mb-0 abcd defg hikjk lmnop">**Needed this Value only**</h4>
                  </div>

I really appreciate is somebody can help.

Below what I am looking for :

Got HTML from CURL request

Than,

    $html = new simple_html_dom();
    $html->load($content);

$html->find('h4') as $h4)

$value = $h4->???????; --> what should be the value ??

PHP has a builtin DOM parser, you don't need a third-party library for a simple lookup:

<?php

$input = '<div class="col-md-6 mb-6 mb-md-3">
    <h3 class="mb-3">testing 1</h5>
    <h4 class="mb-0 abcd defg hikjk lmnop">Not needed this Value</h4>
  </div>

  <div class="col-md-6 mb-6 mb-md-3">
    <h3 class="mb-3">testing 2 </h5>
    <h4 class="mb-0 abcd defg hikjk lmnop">Not needed this Value</h4>
  </div>

  <div class="col-md-6 mb-6 mb-md-3">
    <h3 class="mb-3">testing 3</h5>
    <h4 class="mb-0 abcd defg hikjk lmnop">Not needed this Value</h4>
  </div>

  <div class="col-md-6 mb-6 mb-md-3">
    <h3 class="mb-3">testing</h5>
    <h4 class="mb-0 abcd defg hikjk lmnop">**Needed this Value only**</h4>
  </div>';

$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($input);
libxml_use_internal_errors(false);

var_dump($dom->getElementsByTagName('h4')[3]->nodeValue);

Demo / Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM