It's Hard to Make Money Online Without This Tool - Market Samurai

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

Filed under Internet Marketing · Tagged with , , , , ,

Comments

18 Responses to “PHP Text Spinner”
  1. Lee Says:

    I’m going to try it and see. Thanks so much for sharing it with your readers.

  2. Simple Seo Says:

    Wow, that is very clean.
    Shortest spin code I have ever seen by far.

    Thanks!

  3. Shane Hunter Says:

    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!!

  4. admin Says:

    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.

  5. Shane Hunter Says:

    Great! That worked, Thanks! =)

  6. Brian @ Increase Hair Growth Says:

    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?

  7. admin Says:

    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.

  8. seattle seo consultant Says:

    nice looking code, now on to the real fun: integrating a synonyms database!

  9. chris Says:

    Cheers nice code, about to convert this to c sharp
    thanks!

  10. Gary Roberts Art Says:

    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?

  11. admin Says:

    Hi Gary, thanks. The feed is at http://www.madimmarketing.com/feed/ you should be able to add it to your feed reader.

  12. Ab Machine Says:

    very cool code.. thanks..

  13. Leticia Says:

    Thanks, works like a charm!

  14. Business Card Says:

    Very nice recursive code. Very well implemented.

  15. Paolo Says:

    deym, shortest spinner code i’ve seen yet the best and effective! thankx man!

  16. Nicole Says:

    Great! That worked, Thanks! =)

  17. Nicolas Says:

    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

  18. Brian Ferris Says:

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

    http://www.freegaming.de/Free-Text-Spinner-Tool-Online.html

    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

    Greetings!

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!