Search code examples
phpdomdocumentgetelementsbytagname

how can I get the value by tagnames in php?


could you give a suggestion to my code , please ?

$string = "<li>CIs = <a href="http://localhost/itop/web/pages/UI.pdomhp?operation=details&class=FunctionalCI&id=49&c[menu]=ConfigManagementOverview" title="Functional CI::49">Sep Console04</a>, 42 49, Sep11, Sep Console04<br/><a href="http://localhost/itop/web/pages/UI.php?operation=details&class=FunctionalCI&id=50&c[menu]=ConfigManagementOverview" title="Functional CI::50">Sep Console05</a>, 42 50, Sep11, Sep Console05<br/><a href="http://localhost/itop/web/pages/UI.php?operation=details&class=FunctionalCI&id=53&c[menu]=ConfigManagementOverview" title="Functional CI::53">Sep Engine CO04</a>, 42 53, Sep11, Sep Engine CO04</li>";
$doc = new DOMDocument(); 
$doc->loadHtml($string);
$result_data = $doc->getElementsByTagName('li');

But, I couldn't get the correct result . thanks all of you !

Best regards,

Anwar


Solution

  • I think you're really close

    The following will do the trick:

    $string = '<li>CIs = <a href="http://localhost/itop/web/pages/UI.pdomhp?operation=details&class=FunctionalCI&id=49&c[menu]=ConfigManagementOverview" title="Functional CI::49">Sep Console04</a>, 42 49, Sep11, Sep Console04<br/><a href="http://localhost/itop/web/pages/UI.php?operation=details&class=FunctionalCI&id=50&c[menu]=ConfigManagementOverview" title="Functional CI::50">Sep Console05</a>, 42 50, Sep11, Sep Console05<br/><a href="http://localhost/itop/web/pages/UI.php?operation=details&class=FunctionalCI&id=53&c[menu]=ConfigManagementOverview" title="Functional CI::53">Sep Engine CO04</a>, 42 53, Sep11, Sep Engine CO04</li>';
    $doc = new DOMDocument(); 
    $doc->loadHtml($string);
    $liList = $doc->getElementsByTagName('li');
    $result_data = array();
    foreach ($liList as $li) {
        $result_data[] = $li->nodeValue;
    }
    

    see: Get ul li a string values and store them in a variable or array php