gdianov/araneus

Araneus 是一个用于灵活解析不同来源数据的 PHP 库

v1.0.0 2019-02-25 14:37 UTC

This package is auto-updated.

Last update: 2024-09-25 05:03:24 UTC


README

Araneus 是一个用于灵活解析不同来源数据的 PHP 库

支持的来源: docx, txt, http 资源

  • 所需最低 PHP 版本 >= PHP 7.0。

安装使用命令:composer require gdianov/araneus

如何使用?

  1. 创建规则
<?php

require_once 'vendor/autoload.php';

//Create new Rule
class TitleRule extends \Araneus\Rules\BaseRule implements  \Araneus\Interfaces\RuleInterface
{
    public function getPattern(): string
    {
        return '|<title[^>]*?>(.*?)</title>|sei';
    }
}
  1. 创建 HTTP 解析器
$parseHttp = new \Araneus\Parser(
  new \Araneus\Http\Http('https://google.com')
);

//Attach created rule
$parseHttp->attachRules(new TitleRule()); //You can attach many rules

$result = $parser->run()->fetch(); //array key = regexp, value = found values     
$result = $parser->run()->fetchRules(); //array of Rule objects

              ...
  1. 创建纯文本解析器
$parseTxt = new \Araneus\Parser(
    new \Araneus\File\FilePlainText(__DIR__.'/dst/txt/demo.txt')
);

$parseTxt->attachRules(
  new NumberRule(), 
  new DirtyWordsRule(),
  new UidRule()
);

$result = $parseTxt->run()->fetch();
  1. 创建 Microsoft Word 文档解析器
$parseDocx = new \Araneus\Parser(
    new \Araneus\File\FileDocument(__DIR__.'/dst/documents/demo.docx')
);

$parseDocx->attachRules(
  new UsersRule(),
  new LinksToBooksRule()
);

$result = $parseDocx->run()->fetch();

您可以通过添加您的来源或通过实现接口:SourceInterface、ContentInterface、RuleInterface 来修改现有的解析器来扩展可能性。