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 相关文章推荐
WML,Apache,和 PHP 的介绍
Oct 09 PHP
php学习 字符串课件
Jun 15 PHP
PHP 存储文本换行实现方法
Jan 05 PHP
php数组函数序列之array_splice() - 在数组任意位置插入元素
Nov 07 PHP
php中的PHP_EOL换行符详细解析
Oct 26 PHP
PHP上传图片进行等比缩放可增加水印功能
Jan 13 PHP
php采集内容中带有图片地址的远程图片并保存的方法
Jan 03 PHP
php单例模式实现方法分析
Mar 14 PHP
简单的自定义php模板引擎
Aug 26 PHP
PHP查看SSL证书信息的方法
Sep 22 PHP
php array_map使用自定义的函数处理数组中的每个值
Oct 26 PHP
PHP实现图的邻接矩阵表示及几种简单遍历算法分析
Nov 24 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防注
2007/01/15 PHP
asp和php下textarea提交大量数据发生丢失的解决方法
2008/01/20 PHP
php实现jQuery扩展函数
2009/10/30 PHP
php设计模式之观察者模式的应用详解
2013/05/21 PHP
探讨如何在PHP开启gzip页面压缩实例
2013/06/09 PHP
php字符串比较函数用法小结(strcmp,strcasecmp,strnatcmp及strnatcasecmp)
2016/07/18 PHP
yii2学习教程之5种内置行为类详解
2017/08/03 PHP
用jscript实现新建和保存一个word文档
2007/06/15 Javascript
JavaScript的eval JSON object问题
2009/11/15 Javascript
基于jquery的可多选的下拉列表框
2012/07/20 Javascript
JavaScript中Function详解
2015/02/27 Javascript
如何实现chrome浏览器关闭页面时弹出“确定要离开此面吗?”
2015/03/05 Javascript
jQuery EasyUI 入门必看
2016/06/03 Javascript
vue中的$emit 与$on父子组件与兄弟组件的之间通信方式
2018/05/13 Javascript
vue单页面应用打开新窗口显示跳转页面的实例
2018/09/21 Javascript
微信小程序中显示倒计时代码实例
2019/05/09 Javascript
Vue 路由间跳转和新开窗口的方式(query、params)
2019/12/25 Javascript
el-table表头根据内容自适应完美解决表头错位和固定列错位
2021/01/07 Javascript
python BeautifulSoup使用方法详解
2013/11/21 Python
Python IDLE 错误:IDLE''s subprocess didn''t make connection 的解决方案
2017/02/13 Python
python实现微信跳一跳辅助工具步骤详解
2018/01/04 Python
python实现多线程行情抓取工具的方法
2018/02/28 Python
pandas全表查询定位某个值所在行列的方法
2018/04/12 Python
Python贪心算法实例小结
2018/04/22 Python
在python中bool函数的取值方法
2018/11/01 Python
Python设计模式之观察者模式原理与用法详解
2019/01/16 Python
python爬虫基础知识点整理
2020/06/02 Python
HTML5中drawImage用法分析
2014/12/01 HTML / CSS
用canvas画心电图的示例代码
2018/09/10 HTML / CSS
世界上最大的在线汽车租赁预订平台:Rentalcars.com(支持中文)
2018/10/12 全球购物
美国帽子俱乐部商店:Hat Club
2019/07/05 全球购物
生物科学专业职业规划书范文
2014/02/11 职场文书
政治学求职信
2014/06/03 职场文书
2014报到证办理个人委托书
2014/10/08 职场文书
MongoDB使用场景总结
2022/02/24 MongoDB
【海涛解说】暗牧也疯狂,牛蛙成配角
2022/04/01 DOTA