从文本文件中提取URL

问题描述:

我有一个文本文件,其中输入了许多不同的URL。从文本文件中提取URL

例如:

file:///g:/somepath/subpath/file.txt 
/somepath/subpath/file.txt 
Http://www.domain.at/path/file.txt 

我需要他们从文件中提取。在文本文件中没有HTML,所以我不能搜索像href。

是否有我可以使用的PHP的RegEx?

+0

您是否尝试过的东西? – 2011-12-15 20:32:16

如果您需要更多帮助,请发布整个文件。从给定的...

<?php 

$string = 'file:///g:/somepath/subpath/file.txt 
dasdsaddasdf 
       dsafddsad   /somepath/subpath/file.txt 
    Http://www.domain.at/path/file.txt adsadsadasd 
'; 

preg_match_all('#([\S]+)\.txt#is', $string, $matches); 

print_r($matches[1]); 

?> 

结果

Array 
(
    [0] => file:///g:/somepath/subpath/file 
    [1] => /somepath/subpath/file 
    [2] => Http://www.domain.at/path/file 
)