Blacklist String Validator Class
Join the DZone community and get the full member experience.
Join For FreeCheck string for forbidden stuff. The constructur needs one single string argument, that contains the URI to the blacklist.txt file. This list contains one word per line of words that must not occur in the string to validate.
validate("blabla")) echo "ok";
else echo "failed";
*/
class CheckContent
{
public $blacklistFN; // The textfile with the list of prohibited expressions
public $content=""; // The content to check - A single string
private $blacklistA=array();
private $isValid=TRUE;
// constructor
public function CheckContent($blacklist)
{
$this->isValid=TRUE;
$this->blacklistFN=$blacklist;
$this->content="EMPTY";
}
// Main Method
public function validate($content)
{
$this->content=$content;
if(!$this->readFile()) return FALSE;
foreach ($this->blacklistA as $a)
{
if(preg_match('/'.$a.'/i',$content)) $this->isValid=FALSE;
}
return $this->isValid;
}
// Read File
private function readFile()
{
$blacklistTemp=array();
$blacklistS=FALSE;
$blacklistS=file_get_contents($this->blacklistFN);
if (!$blacklistS)
{
echo "*** ERROR from CheckContent:: - Could not read blacklist file: ".$this->blacklistFN;
return FALSE;
}
$blacklistTemp=explode("\n",$blacklistS);
// Clean array
$this->blacklistA=array();
// Only lines that contain letters or digits
foreach ($blacklistTemp as $a) if( ereg("[a-zA-Z0-9]",$a) ) array_push($this->blacklistA,trim($a));
sort($this->blacklistA); // Cosmetic ;-)
return TRUE;
}
}
Strings
Data Types
Opinions expressed by DZone contributors are their own.
Trending
-
Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers
-
Integrating AWS With Salesforce Using Terraform
-
SRE vs. DevOps
-
Cucumber Selenium Tutorial: A Comprehensive Guide With Examples and Best Practices
Comments