承接 madkom/regex 相关项目开发

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

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

madkom/regex

Composer 安装命令:

composer require madkom/regex

包简介

PCRE RegEx wrapper around PHP regular expression functions

README 文档

README

Whis package maintain various PCRE element implementations in Object Oriented way. Including Pattern, Matcher, Replacer and Splitter objects.

All methods can throw exceptions: BacktrackLimitException, BadUtf8Exception, BadUtf8OffsetException, InternalException, JitStackLimitException and RecursionLimitException so you don't have to check with preg_last_error() and comparing defines;

PHP 7.0 Build Status Latest Stable Version Total Downloads License Coverage Status Code Climate Issue Count

Installation

Install with Composer

composer require madkom/regex

Usage

Using pattern:

use Madkom\RegEx\Pattern;

$pattern = new Pattern('<((?=[^!])(/)?([^>]+))>');
$pattern->getPattern(); // "/<((?=[^!])(\/)?([^>]+))>/"

Matching pattern:

use Madkom\RegEx\Matcher;
use Madkom\RegEx\Pattern;

$pattern = new Pattern('<((?=[^/]+)[^>]+)>');
$matcher = new Matcher($pattern, 's');

$subject = <<<EOL
<!DOCTYPE html>
<html>
<head><title>Ala ma kota</title></head>
<body><h1>Ala ma kota</h1></body>
</html>
EOL;

$match = $matcher->match($subject);

//array:2 [
//  0 => "<!DOCTYPE html>"
//  1 => "!DOCTYPE html"
//]

Matching pattern capture all matches:

use Madkom\RegEx\Matcher;
use Madkom\RegEx\Pattern;

$pattern = new Pattern('<((?=[^/]+)[^>]+)>');
$matcher = new Matcher($pattern, 's');

$subject = <<<EOL
<!DOCTYPE html>
<html>
<head><title>Ala ma kota</title></head>
<body><h1>Ala ma kota</h1></body>
</html>
EOL;

$match = $matcher->matchAll($subject);

//array:2 [
//  0 => array:6 [
//    0 => "<!DOCTYPE html>"
//    1 => "<html>"
//    2 => "<head>"
//    3 => "<title>"
//    4 => "<body>"
//    5 => "<h1>"
//  ]
//  1 => array:6 [
//    0 => "!DOCTYPE html"
//    1 => "html"
//    2 => "head"
//    3 => "title"
//    4 => "body"
//    5 => "h1"
//  ]
//]

Replace with replacement:

use Madkom\RegEx\Replacer;
use Madkom\RegEx\Pattern;

$pattern = new Pattern('<((?=[^!])(/)?([^>]+))>');
$replacer = new Replacer($pattern, 's');

$subject = <<<EOL
<!DOCTYPE html>
<html>
<head><title>Ala ma kota</title></head>
<body><h1>Ala ma kota</h1></body>
</html>
EOL;

$replaced = $replacer->replace($subject, '<\\2xhtml:\\3>');

//<!DOCTYPE html>\n
//<xhtml:html>\n
//<xhtml:head><xhtml:title>Ala ma kota</xhtml:title></xhtml:head>\n
//<xhtml:body><xhtml:h1>Ala ma kota</xhtml:h1></xhtml:body>\n
//</xhtml:html>

Replace with handler:

use Madkom\RegEx\Replacer;
use Madkom\RegEx\Pattern;

$pattern = new Pattern('<((?=[^!])(/)?([^>]+))>');
$replacer = new Replacer($pattern, 's');

$subject = <<<EOL
<!DOCTYPE html>
<html>
<head><title>Ala ma kota</title></head>
<body><h1>Ala ma kota</h1></body>
</html>
EOL;

$replaced = $replacer->replaceWith($subject, function ($match) {
    return "<{$match[2]}xhtml:{$match[3]}>";
});

//<!DOCTYPE html>\n
//<xhtml:html>\n
//<xhtml:head><xhtml:title>Ala ma kota</xhtml:title></xhtml:head>\n
//<xhtml:body><xhtml:h1>Ala ma kota</xhtml:h1></xhtml:body>\n
//</xhtml:html>

Splitting:

use Madkom\RegEx\Splitter;
use Madkom\RegEx\Pattern;

$subject = "html, simple, kayword, complex keyword, keyword with ; semicolon";

$pattern = new Pattern('([,]\s*)');
$splitter = new Splitter($pattern);
$splitted = $splitter->split($subject);

//array:5 [
//  0 => "html"
//  1 => "simple"
//  2 => "kayword"
//  3 => "complex keyword"
//  4 => "keyword with ; semicolon"
//]

Grepping array:

use Madkom\RegEx\Grepper;
use Madkom\RegEx\Pattern;

$subjects = [
    '<a href="http://madkom.pl">madkom.pl</a>',
    '<a href="http://google.pl">google.pl</a>',
    '<a href="http://bing.com">bing.com</a>',
    '<a href="https://example.pl">example.pl</a>',
    '<a href="https://example.org">example.org</a>',
];

$pattern = new Pattern('(http[s]?://[a-z0-9]+\.pl)');
$grepper = new Grepper($pattern, 'iU');
$grepped = $grepper->grep($subjects);

//array:3 [
//  0 => "<a href="http://madkom.pl">madkom.pl</a>"
//  1 => "<a href="http://google.pl">google.pl</a>"
//  3 => "<a href="https://example.pl">example.pl</a>"
//]

License

The MIT License (MIT)

Copyright (c) 2016 Madkom S.A.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-03-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固