php解析字符串里所有URL地址的方法

 具体如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

<?php
// $html = the html on the page
// $current_url = the full url that the html came from
//(only needed for $repath)
// $repath = converts ../ and / and // urls to full valid urls
function pageLinks($html, $current_url = "", $repath = false){
preg_match_all("/<a.+?href=("|')(?!javascript:|#)(.+?)("|')/i", $html, $matches);
$links = array();
if(isset($matches[2])){
$links = $matches[2];
}
if($repath && count($links) > 0 && strlen($current_url) > 0){
$pathi = pathinfo($current_url);
$dir = $pathi["dirname"];
$base = parse_url($current_url);
$split_path = explode("/", $dir);
$url = "";
foreach($links as $k => $link){
if(preg_match("/^../", $link)){
$total = substr_count($link, "../");
for($i = 0; $i < $total; $i++){
array_pop($split_path);
}
$url = implode("/", $split_path) . "/" . str_replace("../", "", $link);
}elseif(preg_match("/^///", $link)){
$url = $base["scheme"] . ":" . $link;
}elseif(preg_match("/^/|^.//", $link)){
$url = $base["scheme"] . "://" . $base["host"] . $link;
}elseif(preg_match("/^[a-zA-Z0-9]/", $link)){
if(preg_match("/^http/", $link)){
$url = $link;
}else{
$url = $dir . "/" . $link;
}
}
$links[$k] = $url;
}
}
return $links;
}
header("content-type: text/plain");
$url = "http://www.jb51.net";
$html = file_get_contents($url);
// Gets links from the page:
print_r(pageLinks($html));
// Gets links from the page and formats them to a full valid url:
print_r(pageLinks($html, $url, true));

时间: 2024-08-15 11:56:17

php解析字符串里所有URL地址的方法的相关文章

php解析字符串里所有URL地址的方法_php技巧

本文实例讲述了php解析字符串里所有URL地址的方法.分享给大家供大家参考.具体如下: <?php // $html = the html on the page // $current_url = the full url that the html came from //(only needed for $repath) // $repath = converts ../ and / and // urls to full valid urls function pageLinks($ht

php提取字符串中网站url地址的方法_php技巧

本文实例讲述了php提取字符串中网站url地址的方法.分享给大家供大家参考.具体分析如下: 今天写一个问答系统上线之后发现有很多人发链接了,由于业务部门要我们过滤掉网站地址了,下面我给大家分享一个提取字符串url地址函数,代码如下: 复制代码 代码如下: $postInfo['answer2'] ='可以的,商业贷款可摊还36%,公积金贷款可摊还16%|||可以先把账户里的余额提取出来用作首付,然后每个月贷款商业贷款可摊还36%,公积金贷款可摊还16%|||可以的,现在甲类公积金是摊还比例htt

php提取字符串里所有URL的例子

这篇文章主要介绍了php提取字符串中网站url地址的方法,可通过正则匹配查找并提取出URL地址,需要的朋友可以参考下 $postInfo['answer2'] ='学习php的方法很多,通过搜索引擎可以搜索到很多资料,当然也可以进入PHP二次开发博客学习哦:http://www.111cn.net,一起交流学';    preg_match_all("/http:[\/]{2}[a-z]+[.]{1}[a-z\d\-]+[.]{1}[a-z\d]*[\/]*[A-Za-z\d]*[\/]*[A-

php获取字符串中图片url地址正则

例子  代码如下 复制代码 $preg = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/i"; preg_match_all($preg,$str,$res); 这下子,管用多了,不管怎样的代码结构,都能顺利的匹配出图片的路径. 例子  代码如下 复制代码 $str='<p style="padding: 0px; margin-top: 0px; ma

python清除字符串里非字母字符的方法

  本文实例讲述了python清除字符串里非字母字符的方法.分享给大家供大家参考.具体如下: ? 1 2 3 4 5 6 7 8 9 s = "hello world! how are you? 0" # Short version print filter(lambda c: c.isalpha(), s) # Faster version for long ASCII strings: id_tab = "".join(map(chr, xrange(256))

python统计文本字符串里单词出现频率的方法

  本文实例讲述了python统计文本字符串里单词出现频率的方法.分享给大家供大家参考.具体实现方法如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 # word frequency in a text # tested with Python24 vegaseat 25aug2005 # Chinese wisdom ...

JS获取几种URL地址的方法小结

 本篇文章主要是对JS获取几种URL地址的方法进行了总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助 以下为JS获取几种URL地址的方法    代码如下: thisURL = document.URL; thisHREF = document.location.href; thisSLoc = self.location.href; thisDLoc = document.location;   thisTLoc = top.location.href; thisPLoc = parent

JS获取几种URL地址的方法小结_javascript技巧

以下为JS获取几种URL地址的方法 复制代码 代码如下: thisURL = document.URL;thisHREF = document.location.href;thisSLoc = self.location.href;thisDLoc = document.location; thisTLoc = top.location.href;thisPLoc = parent.document.location;thisTHost = top.location.hostname;thisH

js实现ifram取父窗口URL地址的方法_javascript技巧

本文实例讲述了js实现ifram取父窗口URL地址的方法.分享给大家供大家参考.具体实现方法如下: var url=''; try { url = window.top.document.referrer ; } catch(M) { if (window.parent) { try { url = window.parent.document.referrer; } catch(L) { url = ""; } } } if (url === "") { url