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

Displaying spintax formated article on a webpage

Discussion in 'General Scripting Chat' started by mightybh, Jul 12, 2011.

  1. mightybh

    mightybh Jr. VIP Jr. VIP Premium Member

    Joined:
    Feb 27, 2008
    Messages:
    1,029
    Likes Received:
    1,714
    Occupation:
    CEO
    Location:
    UK
    -edit- problem solved

    I have an article in the usual {word1|word2|word3} spintax format. What would be the best way to display it on a simple page? At the moment I rewrite it and rotate the keywords with php but it's not very efficient.

    Thank you!

    -edit-

    Found it!

    PHP:
    <?php 
    function spin($s){ 
        
    preg_match('#\{(.+?)\}#is',$s,$m); 
        if(empty(
    $m)) return $s

        
    $t $m[1]; 

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

        
    $parts explode("|"$t); 
        
    $s preg_replace("+\{".preg_quote($t)."\}+is"$parts[array_rand($parts)], $s1); 

        return 
    spin($s); 


    //Example: 
    echo spin('{This|Here} is {some|a {little|wee} bit of} {example|sample} text.'); 
    ?>
     
    Last edited: Jul 12, 2011