php 正则提取href和text <a class="aavv" target="_blank" href="/dsa/asdas">dsdsadsa</a>怎么用正则preg_match_all 获得href和text内容?很急,谢谢大家了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <?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 textpart 3: </b>matched: <a href=howdy.html>click me</a>part 1: <a href=howdy.html>part 2: click mepart 3: </a> 取href:preg_match_all('/(?<=href=")[\w\d\.:\/]*/',$str,$rs);------------------------------------------------------供参考,以前找的一个 $str = '<a class="aavv" target="_blank" href="/dsa/asdas">dsdsadsa </a>';preg_match_all("/<a[^<>]*href=\"([^\"]+)\"[^<>]*>([^<>]+)<\/a>/i", $str, $matches); 按照 3楼的写法:$str = '<a class="aavv" target="_blank" href="/dsa/asdas">dsdsadsa </a>';preg_match_all("/<a[^<>]*href=\"([^\"]+)\"[^<>]*>([^<>]+)<\/a>/i", $str, $matches);echo $matches[1]; //为href的值echo $matches[2]; //为text的值 $str = '<a class="aavv" target="_blank" href="/dsa/asdas">dsdsadsa </a>'$par = '/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/i';preg_match_all($par, $str, $result);这个是我刚刚用来采集的和你的要求差不多应该对你有用 正则表达式用4楼的,只是在preg_match_all()的flag标志位加上PREG_SET_ORDER,这样..$matches[0][1]为href, $matches[0][2]为innerHTML, $matches[1][1]为下一个href, $matches[1][2]为下一个innerHTML, ...... 三楼的可以达到提取href和text的目的,感谢哈! 听说PHP学了框架才强大? 请问这样的返回情况是什么 phpexcel的问题 看到一好文章 跟大家分享! php新手问。。。 需要上传flash代码,急用 用ip访问css丢失 MYSQL数据库中使用自动编号是哪个字段类型? php4.3.1版中PHP.ini配置include_path问题. 超急~~有关pear的问题~~ 上传图片文件遇到了问题,很初级啊,谢谢高人指点指点! php split问题
// \\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>
preg_match_all('/(?<=href=")[\w\d\.:\/]*/',$str,$rs);
------------------------------------------------------供参考,以前找的一个
$str = '<a class="aavv" target="_blank" href="/dsa/asdas">dsdsadsa </a>';
preg_match_all("/<a[^<>]*href=\"([^\"]+)\"[^<>]*>([^<>]+)<\/a>/i", $str, $matches);
$str = '<a class="aavv" target="_blank" href="/dsa/asdas">dsdsadsa </a>';
preg_match_all("/<a[^<>]*href=\"([^\"]+)\"[^<>]*>([^<>]+)<\/a>/i", $str, $matches);echo $matches[1]; //为href的值
echo $matches[2]; //为text的值
preg_match_all($par, $str, $result);这个是我刚刚用来采集的
和你的要求差不多
应该对你有用
这样..$matches[0][1]为href, $matches[0][2]为innerHTML, $matches[1][1]为下一个href, $matches[1][2]为下一个innerHTML,
......