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 相关文章推荐
递归列出所有文件和目录
Oct 09 PHP
检查php文件中是否含有bom的函数
May 31 PHP
深入php var_dump()函数的详解
Jun 05 PHP
浅析关于PHP位运算的简单权限设计
Jun 30 PHP
php ctype函数中文翻译和示例
Mar 21 PHP
php json转换成数组形式代码分享
Nov 10 PHP
PHP中使用CURL获取页面title例子
Jan 07 PHP
php 类自动载入的方法
Jun 03 PHP
php算法实例分享
Jul 14 PHP
Symfony模板的快捷变量用法实例
Mar 17 PHP
使用PHP+Redis实现延迟任务,实现自动取消订单功能
Nov 21 PHP
PHPExcel实现的读取多工作表操作示例
Apr 14 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调用wsdl文件类型的接口代码分享
2014/11/19 PHP
jQuery+PHP发布的内容进行无刷新分页(Fckeditor)
2015/10/22 PHP
php使用ZipArchive函数实现文件的压缩与解压缩
2015/10/27 PHP
php+ajax无刷新分页实例详解
2015/12/07 PHP
php中引用&amp;的用法分析【变量引用,函数引用,对象引用】
2016/12/12 PHP
Laravel Intervention/image图片处理扩展包的安装、使用与可能遇到的坑详解
2017/11/14 PHP
浅析PHP中的 inet_pton 网络函数
2019/12/16 PHP
PHP ElasticSearch做搜索实例讲解
2020/02/05 PHP
php设计模式之策略模式实例分析【星际争霸游戏案例】
2020/03/26 PHP
JavaScript 数组的 uniq 方法
2008/01/23 Javascript
Jqyery中同等与js中windows.onload的应用
2011/05/10 Javascript
jquery和javascript的区别(常用方法比较)
2013/07/04 Javascript
Javascript操作URL函数修改版
2013/11/07 Javascript
JavaScript String 对象常用方法总结
2016/04/28 Javascript
js输出数据精确到小数点后n位代码
2016/07/02 Javascript
jQuery实现导航滚动到指定内容效果完整实例【附demo源码下载】
2016/09/20 Javascript
JavaScript实现无刷新上传预览图片功能
2017/08/02 Javascript
jQuery实现列表的增加和删除功能
2018/06/14 jQuery
mpvue网易云短信接口实现小程序短信登录的示例代码
2020/04/03 Javascript
[10:49]2014国际邀请赛 叨叨刀塔第二期为真正的电竞喝彩
2014/07/21 DOTA
python logging日志模块的详解
2017/10/29 Python
python3+django2开发一个简单的人员管理系统过程详解
2019/07/23 Python
tensorflow-gpu安装的常见问题及解决方案
2020/01/20 Python
Python连接Oracle之环境配置、实例代码及报错解决方法详解
2020/02/11 Python
pyecharts在数据可视化中的应用详解
2020/06/08 Python
win10下python3.8的PIL库安装过程
2020/06/08 Python
浅析rem和em和px vh vw和% 移动端长度单位
2016/04/28 HTML / CSS
澳大利亚办公室装修:JasonL Office Furniture
2019/06/25 全球购物
指针和引用有什么区别
2013/01/13 面试题
营销与策划应届生求职信
2013/11/04 职场文书
国际商务系学生个人的自我评价
2013/11/26 职场文书
我的大学生活演讲稿
2014/04/25 职场文书
2015年民主评议党员工作总结
2015/05/19 职场文书
社区安置帮教工作总结2015
2015/05/20 职场文书
Go语言中break label与goto label的区别
2021/04/28 Golang
MySQL 十大常用字符串函数详解
2021/06/30 MySQL