phphtmldomnodevalue

PHP Simple HTML Dom: Get childNodes nodeValue?


a.php:

<ul id="ul1">
    <li id="pt1">Point 1
         <ul id="ul2">
             <li id="pt11">Point 1.1</li>
             <li id="pt12">Point 1.2</li>
                <pre class="CodeDisplay">
                some codes
                </pre>
             <li id="ref">Reference: <a href="link.html" target="_blank">link</a></li>
         </ul>
    </li> 
</ul>

I would like to get the nodeValue "Point 1" only. In JS, it is:

alert(document.getElementsByTagName("li")[0].childNodes[0].nodeValue);

But I would like to get the nodeValue in PHP (Simple HTML Dom); Here's the code snippet in another PHP page (b.php):

<?php

include('simple_html_dom.php');
$html = file_get_html('http://lifelearning.net63.net/a.php');

// stuck here:
echo $html->getElementsByTagName('ul',0)->getElementsByTagName('li',0)->nodeValue;
//

?>

I have used textContent but it just extracts the content descendents under Point 1. This is not what I want. I only want "Point 1". Any help is appreciated!


Solution

  • u may looking for this

     <?php  $str2 =     ' <ul id="ul1"> ' ;?>
     <?php  $str2 .=    '<li id="pt1"><div>Point 1</div> ' ;?>
     <?php  $str2 .=    ' <ul id="ul2"> ' ; ?>
     <?php  $str2 .=    '     <li id="pt11">Point 1.1</li>' ; ?>
     <?php  $str2 .=    '    <li id="pt12">Point 1.2</li>' ; ?>
     <?php  $str2 .=    '     <pre class="CodeDisplay">' ; ?>
     <?php  $str2 .=    '     some codes' ; ?>
     <?php  $str2 .=    '     </pre>' ; ?>
     <?php  $str2 .=    '    <li id="ref">Reference: <a href="link.html" target="_blank">link</a></li>' ; ?>
     <?php  $str2 .=    '  </ul>' ; ?>
     <?php  $str2 .=    '   </li> ' ; ?>
     <?php  $str2 .=    ' </ul>' ; ?>
    
     <?php
    
     function getTextBetweenTags($string, $tagname) {
         $pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
         preg_match($pattern, $string, $matches);
         return $matches[1];
         }
    
       $txt = getTextBetweenTags($str2, "div");
       echo $txt;
       ?>
    
       will output : -->  Point 1