Last active
May 8, 2020 22:33
-
-
Save mardix/5438589 to your computer and use it in GitHub Desktop.
Voodoo\CanOfSpam, a PHP 5.4 implementation to parse and get random spam text for blog comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* CanOfSpam, a PHP 5.4 implementation to parse and get random spam text for blog comments | |
* -> https://gist.github.com/shanselman/5422230 | |
* | |
* License: MIT | |
* Author : Mardix - http://github.com/mardix | |
* | |
* How to use | |
* | |
* $contents = file_get_contents("path/to/file.txt"); | |
* $canOfSpam = new Voodoo\CanOfSpam($contents); | |
* $randomSpam = $canOfSpam->getRandom(); | |
* | |
*/ | |
namespace Voodoo; | |
class CanOfSpam { | |
private $randomTextPattern = "/{(.*?)}/"; | |
private $spams = []; | |
private $totalSpams = 0; | |
/** | |
* Constructor | |
* @param string $contents | |
*/ | |
public function __construct($contents) | |
{ | |
$this->spams = explode("|\n", $contents); | |
$this->totalSpams = count($this->spams); | |
} | |
/** | |
* Return a random spam | |
* | |
* @return string | |
*/ | |
public function getRandom() | |
{ | |
return preg_replace_callback($this->randomTextPattern, | |
function($match) { | |
$matched = explode("|", $match[1]); | |
return $matched[array_rand($matched, 1)]." "; | |
}, | |
$this->spams[rand(0, $this->totalSpams)] | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment