cod*_*eak 6 php regex preg-replace
我们有一个C2C网站,我们不鼓励在我们的网站上销售品牌产品.我们建立了一个品牌词汇数据库,如Nike和D&G,并制作了一个算法来过滤这些词的产品信息,并禁用产品,如果它包含这些词.
我们当前的算法从提供的文本中删除所有空格和特殊字符,并将文本与数据库中的单词匹配.这些情况需要通过算法捕获并有效捕获:
现在的问题是它还包含以下内容:
如何在捕获真实案例的同时保持效率,可以采取哪些措施来防止这种错误匹配?
编辑
以下是那些更了解代码的人的代码:
$orignal_txt = preg_replace('/&.{0,}?;/', '', (strip_tags($orignal_txt)));
$orignal_txt_nospace = preg_replace('/\W/', '', $orignal_txt);
{
$qry_kws = array("nike", "iphone", "d&g");
foreach($qry_kws as $rs_kw)
{
$no_space_db_kw = preg_replace('/\W/', '', $rs_kw);
if(stristr($orignal_txt_nospace, $rs_kw))
{
$ipr_banned_keywords[] = strtolower($rs_kw);
}
else if(stristr($orignal_txt_nospace, $no_space_db_kw))
{
$ipr_banned_keywords[] = strtolower($rs_kw);
}
}
}
Run Code Online (Sandbox Code Playgroud)
只是在玩....(不用于生产)
$data = array(
"i am nike world",
"i have n ikee shoes",
"i have nikeeshoes",
"i sell i-phone casings",
"i sell iphone-casings",
"you can have iphone",
"rapiD Garment factor",
"rosNIK Electronics",
"Buy you self N I K E",
"B*U*Y I*P*H*O*N*E BABY",
"My Phone Is not available");
$ban = array("nike","d&g","iphone");
Run Code Online (Sandbox Code Playgroud)
范例1:
$filter = new BrandFilterIterator($data);
$filter->parseBan($ban);
foreach ( $filter as $word ) {
echo $word, PHP_EOL;
}
Run Code Online (Sandbox Code Playgroud)
输出1
rapiD Garment factor
rosNIK Electronics
My Phone Is not available
Run Code Online (Sandbox Code Playgroud)
例子2
$filter = new BrandFilterIterator($data,true); //reverse filter
$filter->parseBan($ban);
foreach ( $filter as $word ) {
echo $word, " " , json_encode($word->getBan()) , PHP_EOL;
}
Run Code Online (Sandbox Code Playgroud)
输出2
i am nike world ["nike"]
i have n ikee shoes ["nike"]
i have nikeeshoes ["nike"]
i sell i-phone casings ["iphone"]
i sell iphone-casings ["iphone"]
you can have iphone ["iphone"]
Buy you self N I K E ["nike"]
B*U*Y I*P*H*O*N*E BABY ["iphone"]
Run Code Online (Sandbox Code Playgroud)
使用的班级
class BrandFilterIterator extends FilterIterator {
private $words = array();
private $reverse = false;
function __construct(array $words, $reverse = false) {
$this->reverse = $reverse;
foreach ( $words as $word ) {
$this->words[] = new Word($word);
}
parent::__construct(new ArrayIterator($this->words));
}
function parseBan(array $ban) {
foreach ( $ban as $item ) {
foreach ( $this->words as $word ) {
$word->checkMetrix($item);
}
}
}
public function accept() {
if ($this->reverse) {
return $this->getInnerIterator()->current()->accept() ? false : true;
}
return $this->getInnerIterator()->current()->accept();
}
}
class Word {
private $ban = array();
private $word;
private $parts;
private $accept = true;
function __construct($word) {
$this->word = $word;
$this->parts = explode(" ", $word);
}
function __toString() {
return $this->word;
}
function getTrim() {
return preg_replace('/\W/', '', $this->word);
}
function accept() {
return $this->accept;
}
function getBan() {
return array_unique($this->ban);
}
function reject($ban = null) {
$ban === null or $this->ban[] = $ban;
$this->accept = false;
return $this->accept;
}
function checkMetrix($ban) {
foreach ( $this->parts as $part ) {
$part = strtolower($part);
$ban = strtolower($ban);
$t = ceil(strlen(strtolower($ban)) / strlen($part) * 100);
$s = similar_text($part, $ban, $p);
$l = levenshtein($part, $part);
if (ceil($p) >= $t || ($t == 100 && $p >= 75 && $l == 0)) {
$this->reject($ban);
}
}
// Detect Bad Use of space
if (ceil(strlen($this->getTrim()) / strlen($this->word) * 100) < 75) {
if (stripos($this->getTrim(), $ban) !== false) {
$this->reject($ban);
}
}
return $this->accept;
}
}
Run Code Online (Sandbox Code Playgroud)