0

Im new in PHP and wanna make a little script. I need to parse info from ex. http://sample.com/value.php This is html:

   <ul>
  <li>Value: 100<ul>
   <li>Note: abcd </li>
  </li>
</ul>
<ul>
  <li>Note: adad<ul>
   <li>Note: qweqw </li>
  </li>
</ul>
<ul>
<ul>
  <li>Value: 200<ul>
   <li>Note: abcd </li>
  </li>
</ul>
<ul>

But in my script I need to parse only info with value (Value: 100, Value: 200) website got a lot of code about 200-300 lines. ul and li don't have any class so I can't use getElementsByTagName(). Anybody know how solve that problem ?

  • 1
    Possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – FirstOne Jul 13 '16 at 11:53
  • Use PHP library to parse HTML and don't even think about RegEx – Justinas Jul 13 '16 at 11:53
  • Since you mention `getElementsByTagName()` I presume that you need to parse HTML with javascript? Or can it be done with PHP? – Glavić Jul 13 '16 at 12:06

2 Answers2

0

Parsing html using DOMXpath:

$doc = new DOMDocument();
$doc->loadHTML($YOUR_HTML);
$xpath = new DOMXpath($doc);
$lis = $xpath->query("//li/text()");
if (!is_null($lis)) {
    foreach ($lis as $li) {
        if (preg_match('~^Value: (.+?)$~', $li->nodeValue, $m)) {
            print_r($m);
        }
    }
}

demo

Glavić
  • 42,781
  • 13
  • 77
  • 107
0

use strip_tags — Strip HTML and PHP tags from a string

$myCleanText = strip_tags ($YOUR_HTML));
Martin S.
  • 256
  • 1
  • 10