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


Posted in PHP onApril 03, 2015

本文实例讲述了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($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 = "https://3water.com";
$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));

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
怎样在PHP中通过ADO调用Asscess数据库和COM程序
Oct 09 PHP
基于curl数据采集之单页面采集函数get_html的使用
Apr 28 PHP
探讨如何把session存入数据库
Jun 07 PHP
分割GBK中文遭遇乱码的解决方法
Aug 09 PHP
通过dbi使用perl连接mysql数据库的方法
Apr 16 PHP
php调用google接口生成二维码示例
Apr 28 PHP
PHP根据传来的16进制颜色代码自动改变背景颜色
Jun 13 PHP
Codeigniter框架实现获取分页数据和总条数的方法
Dec 05 PHP
php对象和数组相互转换的方法
May 12 PHP
php模仿qq空间或朋友圈发布动态、评论动态、回复评论、删除动态或评论的功能(中)
Jun 11 PHP
[原创]PHP实现字节数Byte转换为KB、MB、GB、TB的方法
Aug 31 PHP
Yii2框架数据验证操作实例详解
May 02 PHP
php对文件进行hash运算的方法
Apr 03 #PHP
php计算给定时间之前的函数用法实例
Apr 03 #PHP
php实现的mongodb操作类实例
Apr 03 #PHP
PHP中判断文件存在使用is_file还是file_exists?
Apr 03 #PHP
php专用数组排序类ArraySortUtil用法实例
Apr 03 #PHP
Win7下手动安装apache2.2、php5.4笔记
Apr 03 #PHP
PHP中返回引用类型的方法
Apr 03 #PHP
You might like
社区(php&amp;&amp;mysql)五
2006/10/09 PHP
CI框架中site_url()和base_url()的区别
2015/01/07 PHP
Yii2 RESTful中api的使用及开发实例详解
2016/07/06 PHP
thinkPHP5框架自定义验证器实现方法分析
2018/06/11 PHP
用js实现的自定义的对话框的实现代码
2010/03/21 Javascript
利用NodeJS和PhantomJS抓取网站页面信息以及网站截图
2013/11/18 NodeJs
用jQuery模拟select下拉框的简单示例代码
2014/01/26 Javascript
jQuery中的编程范式详解
2014/12/15 Javascript
两种JS实现屏蔽鼠标右键的方法
2020/08/20 Javascript
简单实现js选项卡切换效果
2016/02/03 Javascript
Bootstrap按钮功能之查询按钮和重置按钮
2016/10/26 Javascript
微信小程序中的onLoad详解及简单实例
2017/04/05 Javascript
使用nodejs爬取前程无忧前端技能排行
2017/05/06 NodeJs
详解vue2.0脚手架的webpack 配置文件分析
2017/05/27 Javascript
原生JS实现获取及修改CSS样式的方法
2018/09/04 Javascript
vue父子组件通信的高级用法示例
2019/08/29 Javascript
JavaScript eval()函数定义及使用方法详解
2020/07/07 Javascript
jQuery实现简单轮播图效果
2020/12/27 jQuery
微信小程序视频弹幕发送功能的实现
2020/12/28 Javascript
Python保存MongoDB上的文件到本地的方法
2016/03/16 Python
python删除服务器文件代码示例
2018/02/09 Python
django框架自定义用户表操作示例
2018/08/07 Python
在pycharm中设置显示行数的方法
2019/01/16 Python
python基于socket进行端口转发实现后门隐藏的示例
2019/07/25 Python
Python中if有多个条件处理方法
2020/02/26 Python
利用CSS3实现毛玻璃效果示例源码
2016/09/25 HTML / CSS
html5中的input新属性range使用记录
2014/09/05 HTML / CSS
四川internet信息高速公路(C#)笔试题
2012/02/29 面试题
酒店管理自荐信
2013/10/23 职场文书
幼儿运动会邀请函
2014/01/17 职场文书
幼儿园中班下学期评语
2014/04/18 职场文书
2014年售票员工作总结
2014/11/19 职场文书
同事打架检讨书
2015/05/06 职场文书
2015年幼儿园教育教学工作总结
2015/05/25 职场文书
婚礼必备主持词范本!
2019/07/23 职场文书
Python使用openpyxl模块处理Excel文件
2022/06/05 Python