PHP Text Spinner
You know what text/article spinners are and what they are used for. Usually article spinning is used to spin articles and submit “unique” spun copies to different article directories and other websites. But what if you wanted to spin the text automatically on your website?
Recently I needed such functionality on one of my websites and as a result I’ve created a PHP function that would spin text provided in a spinner syntax. The function turned out to be so simple that I’ve decided to post it here should anyone need it.
As it turns out there are even commercial PHP spinners out there that cost a nice buck – you get to use this one for free! The function is a simple spinner that just spits out a spun text and supports nested syntax:
function spin($s){
preg_match('#\{(.+?)\}#is',$s,$m);
if(empty($m)) return $s;
$t = $m[1];
if(strpos($t,'{')!==false){
$t = substr($t, strpos($t,'{') + 1);
}
$parts = explode("|", $t);
$s = preg_replace("+\{".preg_quote($t)."\}+is", $parts[array_rand($parts)], $s, 1);
return spin($s);
}
//Example:
echo spin('{This|Here} is {some|a {little|wee} bit of} {example|sample} text.');
Enjoy!


I’m going to try it and see. Thanks so much for sharing it with your readers.
Wow, that is very clean.
Shortest spin code I have ever seen by far.
Thanks!