1

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

Anwar Ahmat
  • 219
  • 5
  • 14
  • What are you getting and what is the expected result? Are you getting any error? (cause I suspect you are indeed getting a parser error) – Tivie Mar 06 '15 at 14:38
  • yes, you are right . I got the following error message: " Catchable fatal error: Object of class DOMNodeList could not be converted to string " thanks ! – Anwar Ahmat Mar 06 '15 at 14:49

2 Answers2

1

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

Community
  • 1
  • 1
Youri Nooijen
  • 478
  • 3
  • 13
0

Use single quotes before <li> and after </li>

Also change all & to &amp; in your string, they are special characters that need to be converted to HTML:

$string = str_replace('&','&amp;', $string);
n-dru
  • 9,285
  • 2
  • 29
  • 42