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, strrpos($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!
Edit
(March 19,
2010):
Found a bug with
nesting levels 3 and
up – the code
is updated (using
strrpos instead of
strpos).



February 8th, 2010 at 8:30 pm
I’m going to try it and see. Thanks so much for sharing it with your readers.
March 4th, 2010 at 5:07 pm
Wow, that is very clean.
Shortest spin code I have ever seen by far.
Thanks!
June 30th, 2010 at 12:24 pm
function spin($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 spin($s);
}
$spin2 = spin(‘{#1|#2|#3|#4|#5|#6|#7|#8|#9|#10|#11|#12|#13|#14|#15|#16|#17|#18|#19|#20}’);
$speak = spin(‘{interesting|awesome|amazing|cool|informative|wierd|dope|wicked|neat|swell|inspiring|audacious|fullfilling|kickass|super cool} {site|page|article|link|file|place|zone|method|failure|success story|tactic}’);
What I need to do is call the results of the $spin2 function (ie: #1, #2 etc) and the results of $speak (ie: interesting failure etc…) into a new function using the same spin code:
$awesome = $spin(‘{$spin2|$speak}’);
so that $awesome will return either “#1″ OR “awesome method” etc…
Right now, using the code above, I get a result returned of the actual function
ie: the echo is “$spin2″ or “$speak” instead of the results OF the function.
Hopefully this makes sense, and hopefully someone can give me some useful code here.
Thanks ahead of time!!
June 30th, 2010 at 1:05 pm
Hi Shane,
If I understand you correctly, you just have to drop spin() function from $spin2 and $speak:
$spin2 = ‘{#1|#2|#3|#4|#5|#6|#7|#8|#9|#10|#11|#12|#13|#14|#15|#16|#17|#18|#19|#20}’;
$speak = ‘{interesting|awesome|amazing|cool|informative|wierd|dope|wicked|neat|swell|inspiring|audacious|fullfilling|kickass|super cool} {site|page|article|link|file|place|zone|method|failure|success story|tactic}’;
$awesome = spin(“{$spin2|$speak}”);
Note the double quotes in the above line.
July 8th, 2010 at 6:20 pm
Great! That worked, Thanks! =)