Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php解析带有样式的h标记_Php_Parsing - Fatal编程技术网

php解析带有样式的h标记

php解析带有样式的h标记,php,parsing,Php,Parsing,我找不到任何关于解析此类示例的信息 <h3 style="color:red; font-size:24px;">This contest is still open.</h3> 不要用正则表达式解析HTML。使用真正的HTML解析器。喜欢 或者。为什么不使用?它是为解析HTML而设计的;regex不是 $dom = new DOMDocument(); // Assuming it supports URL, if not, put `file_get_conten

我找不到任何关于解析此类示例的信息

<h3 style="color:red; font-size:24px;">This contest is still open.</h3>

不要用正则表达式解析HTML。使用真正的HTML解析器。喜欢

或者。

为什么不使用?它是为解析HTML而设计的;regex不是

$dom = new DOMDocument();

// Assuming it supports URL, if not, put `file_get_contents()` in there.
$dom->loadHTMLFile('http://www.website.com/contest.php');

foreach( $dom->getElemetsByTagName('h3') as $h3) {
   if ($h3->hasAttribute('style') AND
       $h3->getAttribute('style') == 'color:red; font-size:24px;'
   ) {
      echo $h3->nodeValue;
      break;
   }
}

我同意其他的答案,你不应该使用正则表达式,但鉴于你是,我认为这更接近你想要的

preg_match( '#<h3[^>]+?>(.*?)</h3>#i', $html, $match );
preg#u match('#]+?>(.*?#i',$html,$match);

每次解析H标记时,我都使用style;)对于正则表达式,它很可能由于文本内容中的换行而失败。在这种情况下,
#is
标志会有所帮助。我收到“在非对象上调用成员函数loadHTML()”的错误消息,这是否意味着我使用的url不支持html@FAFAFOHI我塞满了,应该是
loadHTMLFile()
。确保您也有文档。
preg_match( '#<h3[^>]+?>(.*?)</h3>#i', $html, $match );