UPDATE: Please use the new improved version PHP Spintax Class HERE
Internet Marketers often use something they call Spintax. Spintax let’s you wrap text in syntax like this {option 1|option 2|option 3} and when processed, it will randomly select one of the text in between the Pipes to show a random text. This function also allows you to nest Spintax inside of other Spintax blocks.
The code is pretty straight forward so I will not go over it, if you have any questions leave a comment…
$str = 'My {spintax|spuntext} formatted string will return {option 1|option 2|option 3}';
echo SpinTax($str);
/**
* @name SpinTax
* @var str Text containing our {spintax|spuntext}
* @return str Text with random spintax selections
*/
function SpinTax($s) {
preg_match('#{(.+?)}#is',$s,$m);
if(empty($m)) return $s;
$t = $m[1];
if(strpos($t,'{')!==false){
$t = substr($t, strrpos($t,'{') + 1);
}
$parts = explode("|", $t);
$s = preg_replace("+{".preg_quote($t)."}+is", $parts[array_rand($parts)], $s, 1);
return SpinTax($s);
}
Pingback: PHP Spintax Processor | UmeedainTimes.com