jignasa
Filed under - PHP
No Comments so far. Add yours now

Here is the code for smarty plugin for applying random text selection and spinning.

Code you have to place in file /Smarty/plugins/modifier.ranspin.php is as below:

function ranspin($s)
{
preg_match(‘#[(.+?)]#is’,$s,$m);
if(empty($m)) return $s;

$t = $m[1];

if(strpos($t,’[')!==false){
$t = substr($t, strrpos($t,'[') + 1);
}

if(strstr($t, '|'))
{
$parts = explode("|", $t);
$s = preg_replace("+[".preg_quote($t)."]+is”, $parts[array_rand($parts)], $s, 1);

}
else
{
$parts = explode(“~”, $t);
shuffle($parts);
$s = preg_replace(“+[".preg_quote($t)."]+is”, implode(” “,$parts), $s, 1);
}
return ranspin($s);
}

function smarty_modifier_ranspin($s)
{
preg_match(‘#[(.+?)]#is’,$s,$m);
if(empty($m)) return $s;

$t = $m[1];

if(strpos($t,’[')!==false){
$t = substr($t, strrpos($t,'[') + 1);
}

if(strstr($t, '|'))
{
$parts = explode("|", $t);
$s = preg_replace("+[".preg_quote($t)."]+is”, $parts[array_rand($parts)], $s, 1);

}
else
{
$parts = explode(“~”, $t);
shuffle($parts);
$s = preg_replace(“+[".preg_quote($t)."]+is”, implode(” “,$parts), $s, 1);
}
return ranspin($s);
}

Usage:
{“[1~2~3] text1 [A|B|C] text2″|ranspin}

Tags: , ,

Leave your comment

You must be logged in to post a comment.

Share IT © 2018. All rights reserved.