PHP spintax formatting class

October 2, 2011 | Author: procrastinator | Posted in Programming

So you get your spintax (spuntext) formatted text from The Best Spinner or SpinnerChief PRO API but have a small problem now. You cannot show text on your own site before you format it back to the normal decoded version. I made a small research on Google and found a PHP class for the task, but for my purpose I wanted to have different spintax solved. Next block of code represents the class in new form plus presents a wrapper around it to make usage simpler without needing to construct object first and then use getter. I rather just do “one line calls” and process text via static method call.


// file: Spintax.class.php

// usage: echo Spintax::process('{my nested {spintax|spuntext} formatted string|your nested {spintax|spuntext} formatted string}');
class Spintax
{
function process($text) {
$c = new SpintaxBase();
return $c->get($text);
}
}

// modified from: http://ronaldarichardson.com/2011/08/17/recursive-php-spintax-class-2-5/
class SpintaxBase
{
# template left marker, separator and right marker
var $template = array('{', '|', '}');

# main getter
function get($str) {
do {
$str = $this->capture($str);
} while ($this->match($str));
return $str;
}

function capture($str) {
preg_match('/'.$this->template[0].'[^'.$this->template[0].$this->template[2].']+?'.$this->template[2].'/', $str, $match);
$capture = explode($this->template[1], $match[0]);
return str_replace($match[0], preg_replace('/['.$this->template[0].$this->template[2].']/', "", $capture[rand(0,1)]), $str);
}

 

function match($str) {
return preg_match('/'.$this->template[0].'[^'.$this->template[0].$this->template[2].']+?'.$this->template[2].'/', $str, $match);
}
}

Some might ask, why to spin and rephrase your content anyway? Well on Internet Marketing its a well known fact that more you can make unique content to your site, more pages you get indexed which causes more people to find your site by search engine result pages. Spun content can be used to republish your own, your clients or other people content on article directories, blogs, hubs and so forth. Although its not forbidden to send same article to several web properties (if they happen to accept it!), its not a good idea because duplicate content decreases your changes to get offsite content indexed on search engines. On other words duplicate content is competing directly with other people content and only some amount of text will pass for example Googles filters and get indexed.

See more PHP programming tutorials and tips from php-apps.appspot.com

Author: procrastinator

Internet Marketing enthusiastic, SOME addict and PHP application developer with variety of other interests in life.

This author has published 1 articles so far.

Leave a Reply