Back To Normal
Click Here To Subscribe Via Email

Subscribe To Our E-Mail Newsletter

Friday, August 22, 2014

PHP Spintax Class (with Nested Spinning)


Couch ModePrint It


Hi there. I am sharing a simple and easy to use PHP Spintax class which supports nested spinning too.
You can use this code in your web apps.

It has been written by Jason Davis (codedevelopr.com).

class.spintax.php:

/**
 * Spintax - A helper class to process Spintax strings.
 * @name Spintax
 * @author Jason Davis - http://www.codedevelopr.com/
 */
class Spintax
{
    public function process($text)
    {
        return preg_replace_callback(
            '/\{(((?>[^\{\}]+)|(?R))*)\}/x',
            array($this, 'replace'),
            $text
        );
    }
 
    public function replace($text)
    {
        $text = $this->process($text[1]);
        $parts = explode('|', $text);
        return $parts[array_rand($parts)];
    }
}


The Usage:

$message="Hi {buddy|friend|dear}, this message has been spun";
$spintax = new Spintax();
echo $spintax->process($message);



Let me know if you have any thoughts ;)

:D
:)
:[
;)
:D
:O
(6)
(A)
:'(
:|
:o)
8)
(K)
(M)