从p标签获取元素

时间:2016-07-24 14:45:17

标签: php html xpath domdocument

如何使用DOMDocument从ID为In [17]: np.dot(A.T, A) Out[17]: matrix([[25, 18], [18, 13]]) 的p标记内的<p>标记中获取内容?

以下是代码:

categories

例如,我的HTML可能如下所示:

<?php
   ini_set('max_execution_time', 300);
   //error_reporting(0);
   $errmsg_arr = array();
   $errflag = false;

   function getState($string)
   {
     $ex = explode(" ",$string."  ");
     return $ex[1];
   }
   $xml = "";
   $xml .= '<?xml version="1.0" encoding="UTF-8" ?>';
   $xml .= '
     <tv generator-info-name="www.mysite.com/xmltv">';
   $baseUrl = file_get_contents('www.myscript.com/get-listing.php');

   $domdoc = new DOMDocument();
   $domdoc->strictErrorChecking = false;
   $domdoc->recover=true;
   $domdoc->loadHTML($baseUrl);
?>

我正在尝试提取以下文字:

<p id='categories'>Sports</p>

所以当我尝试这个时:

Sports

echo $ p;

它不会从ID $p = $domdoc->getElementById('categories')->getElementsByTagName('p')->item(0); 中提取内容。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

仅对于那个html,你可以这样做:

echo $domdoc->getElementById('categories')->nodeValue;

输出:

  

运动

在行动here中查看。

由于您在<p>标记内<p> DOMDocument标记,$baseUrl = ' <p id="categories"> <p>Sports</p> </p>'; $baseUrl = str_replace('<p', '<div', $baseUrl); $baseUrl = str_replace('<p>', '<div>', $baseUrl); $baseUrl = str_replace('</p>', '</div>', $baseUrl); $domdoc = new DOMDocument(); $domdoc->loadHTML($baseUrl); echo $domdoc->getElementById('categories')->getElementsByTagName('div')->item(0)->nodeValue; 有点疯狂。根据具体情况,您可以执行以下解决方法:

@Test
void testFooThrowsIndexOutOfBoundsException() {    
  Throwable exception = expectThrows( IndexOutOfBoundsException.class, foo::doStuff );

  assertEquals( "some message", exception.getMessage() );
}

输出与以前相同。