Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.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获取页面的HTML源代码_Php_Html - Fatal编程技术网

使用PHP获取页面的HTML源代码

使用PHP获取页面的HTML源代码,php,html,Php,Html,如果我有html文件: <!doctype html> <html> <head></head> <body> <!-- Begin --> Important Information <!-- End --> </body> </head> </html> 重要信息 如何使用PHP从文件中获取字符串“重要信息”在这个简单的

如果我有html文件:

<!doctype html>
 <html>
  <head></head>
   <body>
    <!-- Begin -->
    Important Information
    <!-- End -->
   </body>
  </head>
 </html>

重要信息

如何使用PHP从文件中获取字符串“重要信息”

在这个简单的示例中,您可以打开文件并执行
fgets()
,直到找到带有
的行,然后保存这些行,直到找到

如果HTML在变量中,您可以执行以下操作:

<?php
$begin = strpos($var, '<!-- Begin -->') + strlen('<!-- Begin -->'); // Can hardcode this with 14 (the length of your 'needle'
$end   = strpos($var, '<!-- End -->');

$text = substr($var, $begin, ($end - $begin));

echo $text;
?>

如果您已经对解析进行了排序,只需使用。您可以向它传递一个URL,它将返回在URL处找到的内容,在本例中是html。或者,如果您在本地拥有该文件,您可以将文件路径传递给它。

您可以通过此路径获取“HTML”

在DOM上执行任何操作,然后读取以下文档:

看一看,我担心的不是(大部分)解析,而是首先获取代码。如何将“重要信息”转换为php$变量?标题很容易误导,你不是“获取HTML源代码”。你只是获取文本。不,对不起,我是,如何将index.html转换为php$变量?如何将fgets与$var一起使用?不要忘记前面的空格
$var
是一个包含html内容的字符串吗?在
DOM
或php中没有
文件获取html
。@Manish simplehtmldom是第三方库,而不是本机php扩展。你在回答中链接到了
DOM
。@gordon::我没提到。抱歉,如果你知道你的答案是错误的,请改正它。届时人们可能会取消否决票。
//file_get_html function from third party library
// Create DOM from URL or file
$html = file_get_html('http://www.example.com/');