# text-match **Repository Path**: coder_home/text-match ## Basic Information - **Project Name**: text-match - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-10-20 - **Last Updated**: 2024-07-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # text-match #### 介绍 使用正则表达式从文本中提取内容。 #### 安装教程 composer require php-tool/text-match #### 使用说明 ###### 使用示例 ```php include __DIR__ . '/./vendor/autoload.php'; use PHPTool\TextMatch\MatchPattern; use PHPTool\TextMatch\Matcher; $html = << example
hello
world

this is a example

E; $pattern = new MatchPattern(); $pattern->setLeftLimit('<{'); //设置正则标签的起始标记 (默认为 '{') $pattern->setRightLimit('}>'); //设置正则标签的结束标记 (默认为 '}') //设置提取规则 $pattern->setPatterns([ '
><{field="words" pattern=".*?"}>
', 'p-tag' =>'

<{field="describes" pattern=".*?"}>

' ]); $matcher = new Matcher($pattern); $result = $matcher->match($html); print_r($result->getData()); // print_r($result->getDataOneValue()); //替换 $content = $matcher->replace($html, ['words'=>'wordddd', 'describes'=>'hello world']); print_r($content); ``` #### 设置提取规则 提取规则通过 **PHPTool\TextMatch\MatchPattern** 对象进行设置 ```php $pattern = new MatchPattern(); $pattern->setLeftLimit('<{'); //设置正则标签的起始标记 (默认为 '{') $pattern->setRightLimit('}>'); //设置正则标签的结束标记 (默认为 '}') //设置提取规则 $pattern->setPatterns([ '
><{field="words" pattern=".*?"}>
', '

<{field="describes" pattern=".*?"}>

' ]); ``` 提取规则中,使用正则标签添加正则表达式,正则标签有两个属性,"pattern"是正则表达式属性,"field"是保存字段,当设置了field后,匹配结果会保存到field设置的字段中。 如果 ```pattern``` 没有设置,会被解析成 ```.*?```