匹配网页内容的网址的正则
我希望把这个网址http://www.425sf.com/的网址都匹配出来
$url = "http://www.425sf.com/";//采集地址$content = file_get_contents($url);$patten = "^((https|http|ftp|rtsp|mms)?://)?(([0-9a-z_!~*'().&=+$%-]+:)?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,4})?((/?)|(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";preg_match_all($patten, $content, $matches);$html = <<<htmlskjdfksjdkf<a href="http://www.baidu.com" id="abc">sdfjk</a><a href="http://hi.baidu.com?info=aaa" id="abcdf">sdfjk</a>html;$r = '/<a[^href]*href="([^"]*)"/i';preg_match_all($r, $html, $a);echo '<pre>';print_r($a[1]);/*Array( [0] => http://www.baidu.com [1] => http://hi.baidu.com?info=aaa)*/
[解决办法]
LS正解:
preg_match_all
(PHP 4, PHP 5)
preg_match_all — 进行全局正则表达式匹配
说明
int preg_match_all ( string $pattern , string $subject , array $matches [, int $flags ] )
在 subject 中搜索所有与 pattern 给出的正则表达式匹配的内容并将结果以 flags 指定的顺序放到 matches 中。
搜索到第一个匹配项之后,接下来的搜索从上一个匹配项末尾开始。
flags 可以是下列标记的组合(注意把 PREG_PATTERN_ORDER 和 PREG_SET_ORDER 合起来用没有意义):
PREG_PATTERN_ORDER
对结果排序使 $matches[0] 为全部模式匹配的数组,$matches[1] 为第一个括号中的子模式所匹配的字符串组成的数组,以此类推。
<?php
preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
"<b>example: </b><div align=left>this is a test</div>",
$out, PREG_PATTERN_ORDER);
print $out[0][0].", ".$out[0][1]."\n";
print $out[1][0].", ".$out[1][1]."\n";
?>
本例将输出:
<b>example: </b>, <div align=left>this is a test</div>
example: , this is a test
因此,$out[0] 包含匹配整个模式的字符串,$out[1] 包含一对 HTML 标记之间的字符串。
PREG_SET_ORDER
对结果排序使 $matches[0] 为第一组匹配项的数组,$matches[1] 为第二组匹配项的数组,以此类推。
<?php
preg_match_all ("|<[^>]+>(.*)</[^>]+>|U",
"<b>example: </b><div align=left>this is a test</div>",
$out, PREG_SET_ORDER);
print $out[0][0].", ".$out[0][1]."\n";
print $out[1][0].", ".$out[1][1]."\n";
?>
本例将输出:
<b>example: </b>, example:
<div align=left>this is a test</div>, this is a test
本例中,$matches[0] 是第一组匹配结果,$matches[0][0] 包含匹配整个模式的文本,$matches[0][1] 包含匹配第一个子模式的文本,以此类推。同样,$matches[1] 是第二组匹配结果,等等。
PREG_OFFSET_CAPTURE
如果设定本标记,对每个出现的匹配结果也同时返回其附属的字符串偏移量。注意这改变了返回的数组的值,使其中的每个单元也是一个数组,其中第一项为匹配字符串,第二项为其在 subject 中的偏移量。本标记自 PHP 4.3.0 起可用。
如果没有给出标记,则假定为 PREG_PATTERN_ORDER。
返回整个模式匹配的次数(可能为零),如果出错返回 FALSE。
Example #1 从某文本中取得所有的电话号码
<?php
preg_match_all ("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",
"Call 555-1212 or 1-800-555-1212", $phones);
?>
Example #2 搜索匹配的 HTML 标记(greedy)
<?php
// \\2 是一个逆向引用的例子,其在 PCRE 中的含义是
// 必须匹配正则表达式本身中第二组括号内的内容,本例中
// 就是 ([\w]+)。因为字符串在双引号中,所以需要
// 多加一个反斜线。
$html = "<b>bold text</b><a href=howdy.html>click me</a>";
preg_match_all ("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/", $html, $matches);
for ($i=0; $i< count($matches[0]); $i++) {
echo "matched: ".$matches[0][$i]."\n";
echo "part 1: ".$matches[1][$i]."\n";
echo "part 2: ".$matches[3][$i]."\n";
echo "part 3: ".$matches[4][$i]."\n\n";
}
?>
本例将输出:
matched: <b>bold text</b>
part 1: <b>
part 2: bold text
part 3: </b>
matched: <a href=howdy.html>click me</a>
part 1: <a href=howdy.html>
part 2: click me
part 3: </a>
[解决办法]
用dom解析,比正则简单。下载及参考地址
http://simplehtmldom.sourceforge.net/
<?php
require("simple_html_dom.php");
$html = file_get_html('http://www.425sf.com/');
foreach($html->find('a') as $element)
echo $element->href . '<br>';
?>
[解决办法]
$html = <<<htmlskjdfksjdkf<a href="http://www.baidu.com" id="abc">sdfjk</a><a href="http://hi.baidu.com?info=aaa" id="abcdf">sdfjk</a><a href="/a.php" id="abcdf">sdfjk</a><a href="https://hi.baidu.com?info=aaa" id="abcdf">sdfjk</a><a href="ftp://hi.baidu.com?info=aaa" id="abcdf">sdfjk</a>html;$r = '/<a[^href]*href="((http|https|ftp):\/\/[^\/|\?|"]*)[^"]*"/i';preg_match_all($r, $html, $a);echo '<pre>';print_r($a[1]);/*Array( [0] => http://www.baidu.com [1] => http://hi.baidu.com [2] => https://hi.baidu.com [3] => ftp://hi.baidu.com)*/
[解决办法]
那是因为它的页面中href属性没有双引号
$r = '/<a[^href]*href="?((http|https|ftp):\/\/[^\/|\?|"| ]*)[^"]*"?/i';
[解决办法]