承接 glicer/simply-html 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

glicer/simply-html

最新稳定版本:1.1.7

Composer 安装命令:

composer require glicer/simply-html

包简介

Simplify add, delete, modify, get nodes in html files by using css selector

README 文档

README

Scrutinizer Code Quality Build Status Coverage Status SensioLabsInsight Dependency Status

Add, delete, modify, read html tags by using css selector.

Get all text, links, summary inside html file.

It's working with PHP DOM Extension and Symfony CssSelector

Installation

This library can be found on Packagist.

The recommended way to install is through composer.

Edit your composer.json and add :

{
    "require": {
       "glicer/simply-html": "dev-master"
    }
}

Install dependencies :

php composer.phar install

How to modify html ?

// Must point to composer's autoload file.
require 'vendor/autoload.php';

use GlHtml\GlHtml;

//read index.html contents
$html = file_get_contents("index.html");

$dom = new GlHtml($html);

//delete all style tags inside head
$dom->delete('head style');

//prepare a new style tag
$style = '<link href="solver.css" type="text/css" rel="stylesheet"></link>';

//add the new style tag
$dom->get("head")[0]->add($style);

//replace a node
$dom->get("span")[0]->replaceMe("<h1></h1>");

//write result in a new html file
file_put_contents("result.html",$dom->html());

How to get all text inside html ?

// Must point to composer's autoload file.
require 'vendor/autoload.php';

use GlHtml\GlHtml;

//read index.html contents
$html = file_get_contents("index.html");

$dom = new GlHtml($html);

//array of string sentences
$sentences = $dom->getSentences();

print_r($sentences);

How to get all links inside html ?

// Must point to composer's autoload file.
require 'vendor/autoload.php';

use GlHtml\GlHtml;

//read index.html contents
$html = file_get_contents("index.html");

$dom = new GlHtml($html);

//array of string url
$links = $dom->getLinks();

print_r($links);

How to extract html headings (h1,h2,...,h6)?

<?php
// Must point to composer's autoload file.
require 'vendor/autoload.php';

use GlHtml\GlHtml;

//read index.html contents
$html = file_get_contents("index.html");

$dom = new GlHtml($html);

//array of GlHtmlSummary object
$summary = $dom->getSummary();

echo $summary[0]->getNode()->getText() . ' ' . $summary[0]->getLevel();

/* 
  extract html headings tree
*/
$summaryTree = $dom->getSummaryTree();

Running Tests

Launch from command line :

vendor\bin\phpunit

License MIT

Contact

Authors : Emmanuel ROECKER & Rym BOUCHAGOUR

Web Development Blog - http://dev.glicer.com

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-19