【问题标题】:find and print specific url or web address using file_get_contents使用 file_get_contents 查找并打印特定的 url 或 web 地址
【发布时间】:2013-06-11 10:05:55
【问题描述】:

我有文件websites.txt,这个文件没有排列文本(它是一个源html代码),我想搜索这个源代码并找到匹配example.com/sub/text的url(所以任何url以 example.com/sub/text 开头应匹配)并打印/回显它们。

我正在使用 file_get_contents 并且只需要打印与 http://www.example.com/sub/text/ 匹配的内容

我尝试了 preg_match 但我不知道如何从 (http://www.example.com/sub/text/) 创建模式

【问题讨论】:

  • 你在尝试什么?请向不知道你在做什么的人解释一下
  • 好吧,我有文件websites.txt,这个文件没有排列文本(它是一个源html代码),我想搜索这个源代码并找到匹配example.com/sub/text的url(所以任何以example.com/sub/text 开头的网址都应该匹配)并打印/回显它们。有可能吗?
  • 能否提供.txt文件?

标签: php


【解决方案1】:

试试这个:

 $pattern="%http://www.+[a-z]+/+[a-z]+/+[a-z]+/%";

if(preg_match_all($pattern,$content,$match)) {


    print_r($match);


}

pdf -> something like this: $pattern="%http://www.+[a-z]+/+[a-z]+/+[a-z]+.pdf%";

【讨论】:

  • 没关系,如果我只想打印/输出以 pdf 结尾的链接,例如 www.mysite.com/store/file.pdf
【解决方案2】:

检查这个以了解目的..复制并在你身边测试..

$contentss = file_get_contents("http://www.ncbi.nlm.nih.gov/pubmed?LinkName=pubmed_pubmed&from_uid=18032633" );

preg_match('/<div class="rprt">(.*)<\/div>/',$contentss,$matches);  
echo $matches[0];

【讨论】:

  • 我需要example.com/sub/text 的模式,你能用上面的模式给我写 preg_match 表达式吗?
  • 这与问题无关。
猜你喜欢
  • 2012-07-30
  • 1970-01-01
  • 1970-01-01
  • 2013-10-15
  • 2015-10-07
  • 2023-04-08
  • 2011-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多