如何通过php中的标记名获取值?

时间:2015-03-06 14:35:01

标签: php domdocument getelementsbytagname

你可以给我的代码一个建议吗?

$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');

但是,我无法得到正确的结果。谢谢你们所有人!

致以最诚挚的问候,

安华

2 个答案:

答案 0 :(得分:1)

我认为你真的很亲近

以下将解决这个问题:

$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;
}

请参阅:Get ul li a string values and store them in a variable or array php

答案 1 :(得分:0)

<li>之前和</li>

之后使用单引号

同时将字符串中的所有&更改为&amp;,它们是需要转换为HTML的特殊字符:

$string = str_replace('&','&amp;', $string);