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){
	if(empty($m)) return $s;

	$t = $m[1];

		$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);

echo spin('{This|Here} is {some|a {little|wee} bit of} {example|sample} text.');


Edit (March 19, 2010): Found a bug with nesting levels 3 and up – the code is updated (using strrpos instead of strpos).

19 thoughts on “PHP Text Spinner”

  1. function spin($s){
    if(empty($m)) return $s;
    $t = $m[1];
    $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!!

  2. 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.

  3. This is interesting, but it raises me some questions, though.

    Where should I put the file?
    Is this applicable in a wordpress blog?
    Will it spend all the contents in my website/blog?

  4. I’m sorry but if you have to ask then most likely you won’t be able to use it.

    It’s not a file, it’s a code snippet. You have to edit your php script to use it. If it’s a simple script then it might be a matter of simple copying and pasting. In case of WordPress it’s a lot more complicated.

  5. This is often a really good blog. I have already been back repeatedly within the last few days and want to subscribe to your feed by using Google but cannot work out the way to do it very well. Do you know of any sort of instructions?

  6. Hi,

    I need to convert the text in another format.

    I need to convert inner spin syntax like [text1|text2] and the outer spin syntax like {text1~text2} etc.

    i.e. if the given spin text is something like {{text1|text2} content1|content2 {text3|text4}} I need to convert it to {[text1|text2} content1~content2 [text3|text4]}

    any Idea

  7. Thanks for Sharing – i have used some parts of your code here:

    I needed an easy free article spinner with or without brackets fast and easy, so i’ve made one by myself.
    The “Text Spinning and Text Randomiziation Online Tool with or without Brackets” – Tool

    – 4 Synonyms per word
    – Brackets / or not
    – 1200 Char Limit (is that to less?)
    – Takes only some seconds
    – No registration (but recaptcha per request)

    Try it if you Like



Leave a Comment