<?php
class
StrFilter{
private
$_white_list
=
array
();
private
$_black_list
=
array
();
private
$_replacement
=
'*'
;
private
$_LTAG
=
'[[##'
;
private
$_RTAG
=
'##]]'
;
public
function
__construct(
$white_list
=
array
(),
$black_list
=
array
(),
$replacement
=
'*'
){
$this
->_white_list =
$white_list
;
$this
->_black_list =
$black_list
;
$this
->_replacement =
$replacement
;
}
public
function
replace(
$content
){
if
(!isset(
$content
) ||
$content
==
''
){
return
''
;
}
$content
=
$this
->protect_white_list(
$content
);
if
(
$this
->_black_list){
foreach
(
$this
->_black_list
as
$val
){
$content
=
str_replace
(
$val
,
$this
->_replacement,
$content
);
}
}
$content
=
$this
->resume_white_list(
$content
);
return
$content
;
}
public
function
check(
$content
){
if
(!isset(
$content
) ||
$content
==
''
){
return
true;
}
$content
=
$this
->protect_white_list(
$content
);
if
(
$this
->_black_list){
foreach
(
$this
->_black_list
as
$val
){
if
(
strstr
(
$content
,
$val
)!=
''
){
return
false;
}
}
}
return
true;
}
private
function
protect_white_list(
$content
){
if
(
$this
->_white_list){
foreach
(
$this
->_white_list
as
$key
=>
$val
){
$content
=
str_replace
(
$val
,
$this
->_LTAG.
$key
.
$this
->_RTAG,
$content
);
}
}
return
$content
;
}
private
function
resume_white_list(
$content
){
if
(
$this
->_white_list){
$content
= preg_replace_callback(
"/\[\[##(.*?)##\]\].*?/si"
,
array
(
$this
,
'getval'
),
$content
);
}
return
$content
;
}
private
function
getval(
$matches
){
return
isset(
$this
->_white_list[
$matches
[1]])?
$this
->_white_list[
$matches
[1]] :
''
;
}
}
?>