我要把一个页面中所有的文字链接取出来..
比如一段HTML里有下面的链接.<a href="http://www.sohu.com" target="_blank">搜狐</a><a href="http://www.sina.com" target="_blank">新浪</a>........
........
........则要显示成为Array
(
[0] => Array
(
[0] => http://www.sohu.com
[1] => 搜狐
) [1] => Array
(
[0] => http://www.sina.com
[1] => 新浪
)
...
...
...
)
比如一段HTML里有下面的链接.<a href="http://www.sohu.com" target="_blank">搜狐</a><a href="http://www.sina.com" target="_blank">新浪</a>........
........
........则要显示成为Array
(
[0] => Array
(
[0] => http://www.sohu.com
[1] => 搜狐
) [1] => Array
(
[0] => http://www.sina.com
[1] => 新浪
)
...
...
...
)
解决方案 »
- 还有人敢拿Access+ASP与MySql+PHP比负载。
- 用session['abd']定义的session变量如何合理的释放资源
- 我用editplus保存成UTF-8格式乱码.但用记事本保存
- php在线人数统计
- 求解!!数据库的问题
- 提请WEB版版主注意!!!!!!!!!!!!!
- php解析问题,请不吝赐教~~谢谢~~
- php3 移到php4下,,不能接收form,,有没有配置方法,,按原来的程序运行?
- 帮忙看看,小问题。APACHE2.044和PHP4.31的配置。
- PHP中请问是否可以得到网卡的MAC地址
- The requested URL /index.php was not found on this server.
- 怎么我一刷新就往数据库里添加一条记录啊?大家帮我看看怎么回事?
$contents = '<a href="http://blog.yodao.com/search?q=%E8%82%A1%E6%8C%874000%E7%82%B9&t=a&keyfrom=163index">股指4000点</a>';
if (1) {
$handle = fopen("http://www.163.com/", "rb");
$contents = "";
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
$contents = iconv("GB2312", "UTF-8", $contents);
}
if (preg_match_all("/<a(?:[^<>]*)href=(?:[\"\']?)([^<>\"\']*)(?:[\"\']?)\s?(?:[^<>]*)>([^<>]*)<\/a>/im", $contents, $res, PREG_SET_ORDER)) {
print_r($res);
}
?>试一下这个吧。测试可以抓出99%以上,不过写在js里面的那种
var link = '<a href=\"http://cha.so.163.com/so.php?key=001001006&q=%D6%D0%B9%FA%B7%AD%D2%EB%CD%F8\">中国翻译网</a>'; 就另当别论了。
结果形如: [58] => Array
(
[0] => <a href="http://co.163.com/">土木在线</a>
[1] => http://co.163.com/
[2] => 土木在线
) [59] => Array
(
[0] => <a href="http://vip.163.com/">VIP邮箱</a>
[1] => http://vip.163.com/
[2] => VIP邮箱
) [60] => Array
(
[0] => <a href="http://biz.163.com/">商业</a>
[1] => http://biz.163.com/
[2] => 商业
)
header("Content-Type: text/html; Charset=utf-8");$str='<a href="http://www.sohu.com" target="_blank">搜狐</a><a href="http://www.sina.com" target="_blank">新浪</a>
';
preg_match_all("/<a href=\"([^\"]*)[^>]*>(.*)<\/a>/",$str,$array);
echo "<pre>";
print_r($array);?>
$array的顺序自己调整