[Plain Text] 纯文本查看 复制代码
int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )
[PHP] 纯文本查看 复制代码
<?php
$pattern = '/(https?|ftps?):\/\/(\w+)\.([^\.\/]+)\.(com|net|org|cn)(\/[\w-\.\/\?\%\&\=]*)?/i';
$subject = "欢迎阅读 http://www.groad.net/bbs/read.php?tid-7070.html 此篇帖子,
欢迎使用谷歌搜索引擎: http://www.google.com/search.php, 大家来下载 ftp://ftp.baidu.com/god.tar.gz";
$i = 1;
if (preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER)) {
foreach($matches as $urls) {
echo "第".$i."个 URL 为: ".$urls[0]."<br>";
echo "第".$i."个 URL 中的协议为: ".$urls[1]."<br>";
echo "第".$i."个 URL 中的主机为: ".$urls[2]."<br>";
echo "第".$i."个 URL 中的域名为: ".$urls[3]."<br>";
echo "第".$i."个 URL 中的顶域为: ".$urls[4]."<br>";
echo "第".$i."个 URL 中的文件为: ".$urls[5]."<br>";
echo "======================<br>";
$i++;
}
}
?>