在php中获取部分html文件

时间:2010-12-15 19:13:03

标签: php html html-parsing

我需要从html文件中获取两件事:

  1. <title></title>
  2. 之间的文字
  3. <body></body>
  4. 之间的文字

    有人知道怎么做吗?这就是我到目前为止所做的:

    $contents = file_get_contents($_GET['file']);
    $title = preg_replace("/.*<title[^>]*>|<\/title>.*/si", "", $file);
    $body = preg_replace("/.*<body[^>]*>|<\/body>.*/si", "", $file);
    

    我需要在文本框中回显标题,在文本区域中回复正文。

1 个答案:

答案 0 :(得分:5)

不要使用正则表达式来解析HTML。见this answer。相反,请使用DOMDocument::LoadHTML