当前位置:网站首页>Php article keyword replacement class
Php article keyword replacement class
2022-04-21 07:55:00 【Mars Murong】
<?php
/*
* Keyword matching class
* $str = " It's van der Sar next year , The next year is the army ";
* $key = new KeyReplace($str,array(" Next year 1"=>'http://baidu.com'," Next year "=>'baidu.com'));
* echo $key->getResultText();
* echo $key->getRuntime();
*/
class KeyReplace
{
private $keys = array();
private $text = "";
private $runtime = 0;
private $url = true;
private $stopkeys = array();
private $all = false;
/**
* @access public
* @param string $text Specify the article to be processed
* @param array $keys Specify dictionary phrases array(key=>url,...) url It could be an array , If it is an array, one of them will be replaced randomly
* @param array $stopkeys Specify stop word array(key,...) The words in this will not be processed
* @param boolean $url true Means to replace with a link, otherwise only replace
* @param boolean $all true Replace all found words , Otherwise, only replace the first time
*/
public function __construct($text='',$keys=array(),$url=true,$stopkeys=array(),$all=false) {
$this->keys = $keys;
$this->text = $text;
$this->url = $url;
$this->stopkeys = $stopkeys;
$this->all = $all;
}
/**
* Get processed articles
* @access public
* @return string text
*/
public function getResultText() {
$start = microtime(true);
$keys = $this->hits_keys();
$keys_tmp = array_keys($keys);
function cmp($a, $b){
if (mb_strlen($a) == mb_strlen($b)) {
return 0;
}
return (mb_strlen($a) < mb_strlen($b)) ? 1 : -1;
}
usort($keys_tmp,"App\Traits\cmp");
foreach($keys_tmp as $key){
if(is_array($keys[$key])){
$url = $keys[$key][rand(0,count($keys[$key])-1)];
}else
$url = $keys[$key];
$this->text = $this->r_s($this->text,$key,$url);
}
$this->runtime = microtime(true)-$start;
return $this->text;
}
/**
* Get processing time
* @access public
* @return float
*/
public function getRuntime() {
return $this->runtime;
}
/**
* Set keywords
* @access public
* @param array $keys array(key=>url,...)
*/
public function setKeys($keys) {
$this->keys = $keys;
}
/**
* Set stop word
* @access public
* @param array $keys array(key,...)
*/
public function setStopKeys($keys) {
$this->stopkeys = $keys;
}
/**
* Set up articles
* @access public
* @param string $text
*/
public function setText($text) {
$this->text = $text;
}
/**
* Used to find the keyword hit in the string
* @access public
* @return array $keys Return the matched word array(key=>url,...)
*/
public function hits_keys(){
$ar = $this->keys;
$ar = $ar?$ar:array();
$result=array();
$str = $this->text;
foreach($ar as $k=>$url){
$k = trim($k);
if(!$k)
continue;
if(strpos($str,$k)!==false && !in_array($k,$this->stopkeys)){
$result[$k] = $url;
}
}
return $result?$result:array();
}
/**
* Used to find the stop word hit in the string
* @access public
* @return array $keys Return the matched word array(key,...)
*/
public function hits_stop_keys(){
$ar = $this->stopkeys;
$ar = $ar?$ar:array();
$result=array();
$str = $this->text;
foreach($ar as $k){
$k = trim($k);
if(!$k)
continue;
if(strpos($str,$k)!==false && in_array($k,$this->stopkeys)){
$result[] = $k;
}
}
return $result?$result:array();
}
/**
* Handle the replacement process
* @access private
* @param string $text Replaced
* @param string $key key word
* @param string $url link
* @return string $text Processed articles
*/
private function r_s($text,$key,$url){
$tmp = $text;
$stop_keys = $this->hits_stop_keys();
$stopkeys = $tags = $a = array();
if(preg_match_all("#<a[^>]+>[^<]*</a[^>]*>#su",$tmp,$m)){
$a=$m[0];
foreach($m[0] as $k=>$z){
$z = preg_replace("#\##s","\#",$z);
$tmp = preg_replace('#'.$z.'#s',"[_a".$k."_]",$tmp,1);
}
};
if(preg_match_all("#<[^>]+>#s",$tmp,$m)){
$tags = $m[0];
foreach($m[0] as $k=>$z){
$z = preg_replace("#\##s","\#",$z);
$tmp = preg_replace('#'.$z.'#s',"[_tag".$k."_]",$tmp,1);
}
}
if(!empty($stop_keys)){
if(preg_match_all("#".implode("|",$stop_keys)."#s",$tmp,$m)){
$stopkeys = $m[0];
foreach($m[0] as $k=>$z){
$z = preg_replace("#\##s","\#",$z);
$tmp = preg_replace('#'.$z.'#s',"[_s".$k."_]",$tmp,1);
}
}
}
$key1 = preg_replace("#([\#\(\)\[\]\*])#s","\\\\$1",$key);
if($this->url)
$tmp = preg_replace("#(?!\[_s|\[_a|\[_|\[_t|\[_ta|\[_tag)".$key1."(?!ag\d+_\]|g\d+_\]|\d+_\]|s\d+_\]|_\])#us",'<a href="'.$url.'">'.$key.'</a>',$tmp,$this->all?-1:1);
else
$tmp = preg_replace("#(?!\[_s|\[_a|\[_|\[_t|\[_ta|\[_tag)".$key1."(?!ag\d+_\]|g\d+_\]|\d+_\]|s\d+_\]|_\])#us",$url,$tmp,$this->all?-1:1);
if(!empty($a)){
foreach($a as $n=>$at){
$tmp = str_replace("[_a".$n."_]",$at,$tmp);
}
}
if(!empty($tags)){
foreach($tags as $n=>$at){
$tmp = str_replace("[_tag".$n."_]",$at,$tmp);
}
}
if(!empty($stopkeys)){
foreach($stopkeys as $n=>$at){
$tmp = str_replace("[_s".$n."_]",$at,$tmp);
}
}
return $tmp;
}
}
Keyword replacement method ( notes : If the replacement array is inconsistent with the length of the article keyword, an error will be reported )
$res = ' The lessor ( Hereinafter referred to as Party A ): xxx Technology Co., Ltd
Contacts : xxx
contact number : xxx
Address : xxx
Lessor's address xxx';
$array = array('home', 'garage','1',3);
$count = substr_count($res, 'xxx');
if ($count > 0) {
$arr = explode('xxx', $res);
$res = '';
for ($i = 0; $i < $count; $i++) {
$res .= $arr[$i] . '"' . $array[$i] . '"';
$i + 1 == $count && ($res .= $arr[$i + 1]);
}
}
echo $res;
版权声明
本文为[Mars Murong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210629261350.html
边栏推荐
- PHP outputs all times between two specified dates
- . net core throws an error and writes it Txt file
- Understanding of data warehouse ODS, DW and DM concepts in data governance platform
- 注解功能補充
- PHP base64 encryption
- kubesphere3.0忘记admin的密码
- Supplément à la fonction d'annotation
- PHP实现AES加密解密
- Use the streaming mode of hutool to read large Excel files
- Time and Duration and Epoch
猜你喜欢

类中的泛型以及泛型类型

Introduction to QT 5.12 actual combat

结合实际聊聊防反接电路(防反接电路总结)

sqlserver 两个表之间进行模糊查询,sqlserver 导入excel数据步骤

IDEA 批量修改变量名、批量替换代码快捷键

【图像融合】基于拉普拉斯金字塔+小波变换实现图像融合含Matlab源码

服务器部署svn环境

MongoDB 实验——数据备份和恢复和数据库优化

Flutter environment construction and other foundations

Bluetooth Profile Specification之(AVRCP篇)5.1AVCTP的连接和释放
随机推荐
torch中permute()函数用法补充内容(矩阵维度变化详细过程)
PHP infinite classification (recursive)
Hackmyvm integrated target | driftingblues-9 (end)
PostgreSQL 15 即将支持SQL 标准中的 MERGE 语句
Implementing AES encryption and decryption with PHP
leetcode 59. Spiral matrix II
Actual combat of cloud native kubesphere multi tenant system
leetcode 203. Remove linked list elements
Command-Line
High availability system design
Laravel EXECL import time becomes digital
注解功能补充
Oracle SQL script record
Record the problems and solutions encountered in using fastjson message converter
高可用系统设计
leetcode 27. Removing Elements
MongoDB 实验——数据备份和恢复和数据库优化
Assembly language -- Method of memory location
Self made webserver from scratch (XV) -- the log base part is completed, and the practical widget double-buffering optimizes the asynchronous write performance
Number