UPDATE: Please use the new improved version PHP Spintax Class HERE
Internet Marketers often use something they call Spintax. Spintax lets 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
$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); } |