我要把一个页面中所有的文字链接取出来..
比如一段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] => 新浪
)
...
...
...
)
解决方案 »
- 在php.ini中设置user_agent有什么作用?会导致,任何用户浏览时,获取到user_agent的值都是php.ini中所指定内容吗?
- php算术小问题
- 网站服务器端编程语言可以实现哪些功能
- header()问题
- 急,在线等(100分)
- 表单的ACTION可不可以设为某个函数?
- 文件问题
- 欲将access转为mysql数据库文件,应该怎么转
- php中英双语网站,url相同,会影响seo吗?
- phpinfo()中没有mysql!!!php文件都配置好了,单纯的php文件也可以运行,就是加载不了拓展,大神求救~
- 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的顺序自己调整