timostamm/url-finder 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

timostamm/url-finder

Composer 安装命令:

composer require timostamm/url-finder

包简介

Find and replace URLs in HTML and CSS documents

README 文档

README

build Packagist PHP Version GitHub tag

Find and replace URLs in HTML, CSS and Markdown documents.

Example input HTML:

<html>
<body>
  <img src="http://domain.tld/img/a.jpg" >
  <div style="background-image: url(./c.jpg);"></div>
  <style>
    .bg-img { background-image: url(../images/h.jpg); }
  </style>
  <script src="https://cdn.tld/angular.min.js"></script>
  <link rel="stylesheet" type="text/css" href="/styles/f.css" />
  ...

Find all jpegs on our domain and move them to /images/

$documentUrl = 'http://domain.tld/products/all.html';
$finder = UrlFinder::create($html, $documentUrl);

foreach ($finder->find('*domain.tld/*.jpg') as $url) {
  $newpath = '/images/' . $url->path->filename();
  $url
    ->replacePath($newpath)
    ->makeAbsolute()
    ->clearHost();
}

$finder->getDocument(); // returns the updated HTML string

The result:

<html>
<body>
  <img src="/images/a.jpg" >
  <div style="background-image: url(/images/c.jpg);"></div>
  <style>
    .bg-img { background-image: url(/images/h.jpg); }
  </style>
  <script src="https://cdn.tld/angular.min.js"></script>
  <link rel="stylesheet" type="text/css" href="/styles/f.css" />
  ...

The UrlFinder takes care of proper quoting of URLs in attributes, url-notations in style-attributes and url- notation within style-tags.

Using the fluid collection interface:

$urls = $finder
  ->find('*') // matches the entire absolute URL
  ->matchHost('*')
  ->matchPath('*')
  ->onlyHttps()
  ->matchFilenameNot('*.less');
  // etc.
  
$urls->count();
$urls->toArray();
$urls->first();
foreach($urls as $url) {}

Updating URLs:

$url->query->set('text', 'value');
$url->clear( Url::CREDENTIALS );

See https://github.com/timostamm/url-builder for documentation of the URL object.

Finding URLs in CSS works exactly the same:

$finder = UrlFinder::create($css, 'http://domain.tld/styles/main.css');
$finder->find()->first()->makeAbsolute();
$finder->getDocument();

Please note that import statements are not suported and you have to follow stylesheet-links yourself.

Markdown support

Markdown support is experimental right now. Caveats:

  • Link / image titles are not supported and will raise an error
  • HTML with links within markdown is ignored
  • Markdown is not available using UrlFinder::create, use new MarkdownUrlFinder()

统计信息

  • 总下载量: 1.65k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固