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 Squid中可缓存的动态网页设计
Sep 17 PHP
PHP新手NOTICE错误常见解决方法
Dec 07 PHP
str_replace只替换一次字符串的方法
Apr 09 PHP
浅析echo(),print(),print_r(),return之间的区别
Nov 27 PHP
CodeIgniter实现更改view文件夹路径的方法
Jul 04 PHP
PHP生成唯一订单号的方法汇总
Apr 16 PHP
php显示时间常用方法小结
Jun 05 PHP
ThinkPHP控制器详解
Jul 27 PHP
PHP的Yii框架中过滤器相关的使用总结
Mar 29 PHP
php session的应用详细介绍
Mar 22 PHP
浅谈php调用python文件
Mar 29 PHP
php 使用 __call实现重载功能示例
Nov 18 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之require/include顺序 推荐
2011/01/02 PHP
php 截取字符串并以零补齐str_pad() 函数
2011/05/07 PHP
php实现在线生成条形码示例分享(条形码生成器)
2013/12/30 PHP
如何在Mozilla Gecko 用Javascript加载XSL
2007/01/09 Javascript
JS类库Bindows1.3中的内存释放方式分析
2007/03/08 Javascript
Javascript 错误处理的几种方法
2009/06/13 Javascript
jquery图片放大镜功能的实例代码
2013/03/26 Javascript
jquery操作select详解(取值,设置选中)
2014/02/07 Javascript
jquery实现增加删除行的方法
2015/02/03 Javascript
Javascript BOM学习小结(六)
2015/11/26 Javascript
BootStrap点击下拉菜单项后显示一个新的输入框实现代码
2016/05/16 Javascript
Validform表单验证总结篇
2016/10/31 Javascript
jQuery实现标签页效果实战(4)
2017/02/08 Javascript
angularjs 缓存的使用详解
2018/03/19 Javascript
vue中音频wavesurfer.js的使用方法
2020/02/20 Vue.js
微信小程序开发(一):服务器获取数据列表渲染操作示例
2020/06/01 Javascript
[01:49]一目了然!DOTA2DotA快捷操作对比第二弹
2014/05/16 DOTA
[02:15]你好,这就是DOTA!
2015/08/05 DOTA
用Python写的图片蜘蛛人代码
2012/08/27 Python
python实现2048小游戏
2015/03/30 Python
Python科学计算之Pandas详解
2017/01/15 Python
Python常用内置模块之xml模块(详解)
2017/05/23 Python
在pycharm中python切换解释器失败的解决方法
2018/10/29 Python
OpenCV实现机器人对物体进行移动跟随的方法实例
2020/11/09 Python
python基于openpyxl生成excel文件
2020/12/23 Python
解决import tensorflow导致jupyter内核死亡的问题
2021/02/06 Python
Html5 localStorage入门教程
2018/04/26 HTML / CSS
canvas进阶之如何画出平滑的曲线
2018/10/15 HTML / CSS
在印度上传处方,在线订购药品:Medlife
2019/03/28 全球购物
java程序员面试交流
2012/11/29 面试题
业务员薪酬管理制度
2014/01/15 职场文书
物流管理专业自荐信
2014/06/23 职场文书
标准大学生职业生涯规划书写作指南
2014/09/18 职场文书
招商引资工作汇报
2014/10/28 职场文书
幼儿园辞职信范文
2015/02/27 职场文书
社区青年志愿者活动总结
2015/05/06 职场文书