1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Spintax class which supports nested and various syntax spinning

Discussion in 'PHP & Perl' started by fimms, Sep 24, 2011.

  1. fimms

    fimms Junior Member

    Joined:
    Apr 30, 2009
    Messages:
    178
    Likes Received:
    59
    Location:
    Fineland
    Home Page:
    So you got your spintax (spuntext) formatted text from The Best Spinner API or SpinnerChief PRO API

    Code:
    http://www.blackhatworld.com/blackhat-seo/php-perl/354215-spinnerchier-api-php-your-free-spinner.html
    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 it had some problems plus for my purpose I wanted to have different spintax solved to keep for example PHP code preserved on text after spin.

    So the next block of code represents the class in a new form plus gives a wrapper around it to make usage simpler without needing to construct object first and then call the getter. I rather just do "one line calls" and process text via static method call whenever its possible and reasonable.

    First create a file called "Spintax.class.php", copy & paste code to it:

    PHP:

    class Spintax
    {
        
    # this is just a wrapper for shorter static calls
        
    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 for default spintax format
        # you can change the format by setting this variable before calling get method for example:
        # $c->template = array('[spin]', '~', '[/spin]');
        
    var $template = array('{''|''}');

        
    # main getter
        
    function get($text) {
            if (
    $text) {
                do {
                    
    $text $this->_capture($text);
                } while (
    $this->_match($text));
            }
            return 
    $text;
        }

        function 
    _capture($text) {
            
    preg_match('/'.$this->template[0].'[^'.$this->template[0].$this->template[2].']+?'.$this->template[2].'/'$text$match);
            if (isset(
    $match[0])) {
                
    $words explode($this->template[1], $match[0]);
                
    $replace preg_replace('/['.$this->template[0].$this->template[2].']/'''$words[rand(0count($words)-1)]);
                
    $text str_replace($match[0], $replace$text);
            }
            return 
    $text;
        }

        function 
    _match($text) {
           return 
    preg_match('/'.$this->template[0].'[^'.$this->template[0].$this->template[2].']+?'.$this->template[2].'/'$text$match);
        }
    }
    Then save file on your server and create another file say "testspin.php" to see how you use the class:

    PHP:

    include 'Spintax.class.php';

    echo 
    Spintax::process('{my nested {spintax|spuntext} formatted string|your recursively handled {spintax|spuntext} formatted string}');

    Save it to the same directory you put "Spintax.class.php" and run "testspin.php" from your web browser or command line.

    Class supports unlimited nested spin blocks, different spin syntax plus reserves php code inside text even it includes same curly brackets as spin syntax. But for better success I use different spintax for that kind situation because if code includes separator mark | if will fail to spin. That separator mark is very common, at least on my PHP code when doing text processing.

    To improve class there could also be a convert method that changes spintax formatted text to the other type of syntax. But I will leave it for future todo's.

    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 exactly same article text 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. In other words duplicate content is competing directly with other people content and only some amount of "texts" will pass for example Googles filters and get indexed.

    I personally use unique pro content on my own sites as much as possible, then spin it to the web2 properties, but I don't care too much of spinning to the lower quality profiles or comments. But if it comes for free and no extra effort, then why not.

    Again, free to use and modify by your own risk :juggle:
     
    • Thanks Thanks x 1
    Last edited: Sep 24, 2011
  2. fimms

    fimms Junior Member

    Joined:
    Apr 30, 2009
    Messages:
    178
    Likes Received:
    59
    Location:
    Fineland
    Home Page:
    Its like development never ends. Next piece of code does:

    1. fixes custom spin tax language bug, that has same chars with regexp
    2. adds template argument to the static process method
    3. centralizes match regexp

    Please see class in function from: http://www.articlemanager.us/tools/spintax/

    PHP:
    // usage: echo Spintax::process('This is {my nested {spintax|spuntext} formatted string|your nested {spintax|spuntext} formatted string} example.');
    class Spintax
    {
        
    # this is just a wrapper for shorter static calls
        
    function process($text$template = array()) {
            
    $c = new SpintaxBase();
            if (
    $template) {
                
    array_walk($template, array($c'filter_template'));
                
    $c->template $template;
            }
            return 
    $c->get($text);
        }
    }

    // modified from: http://ronaldarichardson.com/2011/09/23/recursive-php-spintax-class-3-0/
    class SpintaxBase
    {
        
    # template left marker, separator and right marker for default spintax format
        # you can change the format by setting this variable before calling get method for example:
        # $c->template = array('[spin]', '~', '[/spin]');
        
    public $template = array('{''|''}');
        public 
    $match_str;
        
        
    # sanitize template chars to work with regexp
        
    public function filter_template(&$val) {
            
    $val str_replace(array('['']''/''~'), array('\[''\]''\\/''~'), $val);
        }
        
        
    # main getter
        
    public function get($text) {
            if (
    $text) {
                
    $this->match_str '/'.$this->template[0].'[^'.$this->template[0].$this->template[2].']+?'.$this->template[2].'/';
                do {
                    
    $text $this->_capture($text);
                } while (
    preg_match($this->match_str$text));
            }
            return 
    $text;
        }

        private function 
    _capture($text) {
            
    preg_match($this->match_str$text$match);
            if (isset(
    $match[0])) {
                
    $words explode($this->template[1], $match[0]);
                
    $replace preg_replace('/['.$this->template[0].$this->template[2].']/'''$words[rand(0count($words)-1)]);
                
    $text str_replace($match[0], $replace$text);
            }
            return 
    $text;
        }
    }