【问题标题】:PHP file_get_contents of url from a dynamically created url in htmlPHP file_get_contents 来自 html 中动态创建的 url
【发布时间】:2015-02-25 16:21:39
【问题描述】:

我希望从我已经使用 file_get_contents 获取的页面源代码中动态创建的 URL 中提取 file_get_contents 信息。例如:

$link = $_POST['link'];
$html = file_get_contents('http://www.somesite.com/keywords='.$link."");
$output = file_get_contents(
//A URL that is in the output of $html
);

基本上我希望 PHP 加载一个 HTML 页面,然后单击该页面上的链接,然后获取生成的源代码,这样我就可以将其中的一些解析为变量以供以后使用。有什么想法吗?

【问题讨论】:

  • 不确定您到底遇到了什么。只需使用$html 并获取您需要的链接。如果您不知道如何操作,则需要提供一些 file_get_contents('http://www.somesite.com/keywords='.$link.""); 返回的示例。
  • 如何在不存储任何内容的情况下“稍后使用”?
  • @Bang 问题是“将其中一些解析为变量以供以后使用”

标签: php html regex file-get-contents


【解决方案1】:

使用正则表达式。

$url = preg_match('(https?):\/\/(www\.)?[a-z0-9\.:].*?(?=\s)', $html);

编辑:

这是一个很好的解释:)

What is the best regular expression to check if a string is a valid URL?

【讨论】:

  • 当页面包含很多并且我只想要一个,即动态的并且仅在页面加载时生成时,这不会检查所有 URL 吗?
  • 如果您知道网址在哪里,那么您肯定可以定位到该网址。或者,如果您知道它将是页面上的第一个 url,只需获取 $url 数组中的第一项?
  • 我知道它会出现在哪里,但不确定如何专门针对它。我知道它之前或之后有什么东西
  • @WindowsDan 你能发布一个这些数据的样例吗?
猜你喜欢
  • 1970-01-01
  • 2015-07-16
  • 2014-02-17
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-19
相关资源
最近更新 更多