如何通过PHP将HTML页面作为字符串获取?
I am fetching some info via PHP from a webpage using simple_php_dom
and curl. The problem is that the page is not built correctly so the DOM object contains erroneous info.
我正在使用simple_php_dom和curl从网页上通过PHP获取一些信息。问题是页面未正确构建,因此DOM对象包含错误信息。
How can I get the HTML file as a string in a PHP var so that I can run a regular expression through it?
如何将HTML文件作为PHP var中的字符串获取,以便我可以通过它运行正则表达式?
Curl doesn't work as it is ignoring the bad part.
simple_html_dom.php
has the same issue.
wget
doesn't work since I don't have permissions for it on the server.
卷曲不起作用,因为它忽略了坏部分。 simple_html_dom.php有同样的问题。 wget不起作用,因为我在服务器上没有权限。
3 个解决方案
#1
12
file_get_contents — Reads entire file into a string
file_get_contents - 将整个文件读入字符串
string file_get_contents (
string $filename [, int $flags= 0 [, resource $context [, int $offset= -1 [, int $maxlen= -1 ]]]]
)
from the manual:
从手册:
This function is similar to file(), except that file_get_contents() returns the file in a string, starting at the specified offset up to maxlen bytes. On failure, file_get_contents() will return FALSE.
此函数类似于file(),但file_get_contents()以字符串形式返回文件,从指定的偏移量开始直到maxlen字节。失败时,file_get_contents()将返回FALSE。
file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.
file_get_contents()是将文件内容读入字符串的首选方法。如果操作系统支持,它将使用内存映射技术来提高性能。
And it works both with webpages and files. You can grab the HTML, just by using "http://whatever.com/page.html" as $filename.
它适用于网页和文件。您可以使用“http://whatever.com/page.html”作为$ filename来获取HTML。
更多相关文章
- php 基本的常用字符串函数
- 一个关于用php输出文件的问题(急)
- php中的文件包含
- 用PHP计算字符串中元音的简单方法?
- 使用php浏览文件时,编写文件的完整路径
- composer不能生成sf2 autoload文件。
- include一个php文件,经常会失败,请问需要注意些什么?
- nginx + fastcgi php配置下,安全的文件上存路径设置
- 使用“.html”链接到.html.php文件 - 这有什么神奇之处?