承接 kuria/simple-html-parser 相关项目开发

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

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

kuria/simple-html-parser

Composer 安装命令:

composer require kuria/simple-html-parser

包简介

Minimalistic HTML parser

README 文档

README

Minimalistic HTML parser.

https://travis-ci.com/kuria/simple-html-parser.svg?branch=master

Note

If you need advanced DOM manipulation, consider using kuria/dom instead.

Contents

Features

  • parsing opening tags
  • parsing closing tags
  • parsing comments
  • parsing DTDs
  • extracting parts of HTML content
  • determining encoding of HTML documents
  • handling "raw text" tags (<style>, <script>, <noscript>, etc.)

Requirements

  • PHP 7.1+

Usage

Creating the parser

<?php

use Kuria\SimpleHtmlParser\SimpleHtmlParser;

$parser = new SimpleHtmlParser($html);

Iterating elements

The parser implements Iterator so it can be traversed using the standard iterator methods.

<?php

foreach ($parser as $element) {
    print_r($element);
}
<?php

$parser->rewind();

if ($parser->valid()) {
    print_r($parser->current());
}

Element array structure

Each element is an array with the following keys. Some keys are available only for specific element types.

Key Types Description
type any The type of the element, see Element types
start any Byte offset at which the element begins
end any Byte offset at which the element ends
name

SimpleHtmlParser::OPENING_TAG,

SimpleHtmlParser::CLOSING_TAG

Tag name (e.g. div), see Tag name and attribute normalization
attrs SimpleHtmlParser::OPENING_TAG Array with the opening tag's attributes, see Tag name and attribute normalization
symbol SimpleHtmlParser::OTHER !, ? or /

Element types

  • SimpleHtmlParser::COMMENT - a comment, e.g. <!-- foo -->
  • SimpleHtmlParser::OPENING_TAG - an opening tag, e.g. <span class="bar">
  • SimpleHtmlParser::CLOSING_TAG - a closing tag, e.g. </span>
  • SimpleHtmlParser::OTHER - special element, e.g. doctype, XML header
  • SimpleHtmlParser::INVALID - invalid or incomplete tags

Tag name and attribute normalization

Tag and attribute names that contain only ASCII characters are lowercased.

Managing parser state

The state methods can be used to temporarily store and/or revert state of the parser.

  • pushState() - push current state of the parser onto the stack
  • popState() - pop (discard) state stored on top of the stack
  • revertState() - pop and restore state stored on top of the stack
  • countStates() - count the number of states currently on the stack
  • clearStates() - discard all states

getHtml() - get HTML content

The getHtml() method may be used to get the entire HTML content or HTML of a single element.

<?php

$parser->getHtml(); // get entire document
$parser->getHtml($element); // get single element

getSlice() - get part of the HTML

The getSlice() method returns a part of the HTML content.

Returns an empty string for negative or out-of-bounds ranges.

<?php

$slice = $parser->getSlice(100, 200);

getSliceBetween() - get content between 2 elements

The getSliceBetween() method returns a part of the HTML content that is between 2 elements (usually opening and closing tag).

<?php

$slice = $parser->getSliceBetween($openingTag, $closingTag);

getLength() - get length of the HTML

The getLength() returns total length of the HTML content.

getEncoding() - determine encoding of the HTML document

The getEncoding() method attempts to determine encoding of the HTML document.

If the encoding cannot be determined or is not supported, the fallback encoding will be used instead.

This method does not alter the parser's state.

getEncodingTag() - find the encoding-specifying meta tag

The getEncodingTag() method attempts to find the <meta charset="..."> or <meta http-equiv="Content-Type" content="..."> tag in the first 1024 bytes of the HTML document.

Returns NULL if the tag was not found.

This method does not alter the parser's state.

usesFallbackEncoding() - see if the fallback encoding is being used

The usesFallbackEncoding() indicates whether the fallback encoding is being used. This is the case when the encoding is not specified or is not supported.

This method does not alter the parser's state.

setFallbackEncoding() - set fallback encoding

The setFallbackEncoding() method specifies an encoding to be used in case the document has no encoding specified or specifies an unsupported encoding.

The fallback encoding must be supported by htmlspecialchars().

getDoctypeElement() - find the doctype element

The getDoctypeElement() method attempts to find the doctype in the first 1024 bytes of the HTML document.

Returns NULL if no doctype was found.

escape() - escape a string

The escape() method escapes a string using htmlspecialchars() using the HTML document's encoding.

find() - match a specific element

The find() method attempts to find a specific element starting from the current position, optionally stopping after a given number of bytes.

Returns NULL if no element was matched.

<?php

$element = $parser->find(SimpleHtmlParser::OPENING_TAG, 'title');

getOffset() - get current offset

The getOffset() method returns the current parser offset in bytes.

Example: Reading document's title

<?php

$html = <<<HTML
<!doctype html>
<meta charset="utf-8">
<title>Foo bar</title>
<h1>Baz qux</h1>
HTML;

$parser = new SimpleHtmlParser($html);

$titleOpen = $parser->find(SimpleHtmlParser::OPENING_TAG, 'title');

if ($titleOpen) {
    $titleClose = $parser->find(SimpleHtmlParser::CLOSING_TAG, 'title');

    if ($titleClose) {
        $title = $parser->getSliceBetween($titleOpen, $titleClose);

        var_dump($title);
    }
}

Output:

string(7) "Foo bar"

kuria/simple-html-parser 适用场景与选型建议

kuria/simple-html-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 33.75k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「comment」 「parser」 「html」 「tag」 「encoding」 「charset」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kuria/simple-html-parser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kuria/simple-html-parser 我们能提供哪些服务?
定制开发 / 二次开发

基于 kuria/simple-html-parser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-01-13