Skip to content

Instantly share code, notes, and snippets.

@mardix
Last active May 8, 2020 22:33
Show Gist options
  • Save mardix/5438589 to your computer and use it in GitHub Desktop.
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
<?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