Smart Spinner Outputting all the available phrases

This script is extracting all the spinal content from a given content.



# Similar to str_replace, but only replaces the first instance of the needle
function str_replace_first($find, $replace, $string)
{
    # Ensure we are dealing with arrays
    if(!is_array($find))
    {
        $find = array($find);
    }

    if(!is_array($replace))
    {
        $replace = array($replace);
    }

    foreach($find as $key => $value)
    {
        if(($pos = strpos($string, $value)) !== false)
        {
            # If we have no replacement make it empty
            if(!isset($replace[$key]))
            {
                $replace[$key] = '';
            }

            $string = mb_substr($string, 0, $pos).$replace[$key].mb_substr($string, $pos + mb_strlen($value));
        }
    }

    return $string;
}

# Finds all instances of a needle in the haystack and returns the array
function strpos_all($haystack, $needle)
{
    $offset = 0;
    $i      = 0;
    $return = false;
   
    while(is_integer($i))
    {   
        $i = strpos($haystack, $needle, $offset);
       
        if(is_integer($i))
        {
            $return[]   = $i;
            $offset     = $i + mb_strlen($needle);
        }
    }

    return $return;
}


function spin($string, $seedPageName = true, $openingConstruct = '{', $closingConstruct = '}')
{
    # If we have nothing to spin just exit
    if(strpos($string, $openingConstruct) === false)
    {
        return $string;
    }

    # Find all positions of the starting and opening braces
    $startPositions = strpos_all($string, $openingConstruct);
    $endPositions   = strpos_all($string, $closingConstruct);

    # There must be the same number of opening constructs to closing ones
    if($startPositions === false OR count($startPositions) !== count($endPositions))
    {
        return $string;
    }

    # Optional, always show a particular combination on the page
    if($seedPageName)
    {
        mt_srand(crc32($_SERVER['REQUEST_URI']));
    }

    # Might as well calculate these once
    $openingConstructLength = mb_strlen($openingConstruct);
    $closingConstructLength = mb_strlen($closingConstruct);

    # Organise the starting and opening values into a simple array showing orders
    foreach($startPositions as $pos)
    {
        $order[$pos] = 'open';
    }
    foreach($endPositions as $pos)
    {
        $order[$pos] = 'close';
    }
    ksort($order);

    # Go through the positions to get the depths
    $depth = 0;
    $chunk = 0;
    foreach($order as $position => $state)
    {
        if($state == 'open')
        {
            $depth++;
            $history[] = $position;
        }
        else
        {
            $lastPosition   = end($history);
            $lastKey        = key($history);
            unset($history[$lastKey]);

            $store[$depth][] = mb_substr($string, $lastPosition + $openingConstructLength, $position - $lastPosition - $closingConstructLength);
            $depth--;
        }
    }
    krsort($store);

    # Remove the old array and make sure we know what the original state of the top level spin blocks was
    unset($order);
    $original = $store[1];

    # Move through all elements and spin them
    foreach($store as $depth => $values)
    {
        foreach($values as $key => $spin)
        {
            # Get the choices
            $choices = explode('|', $store[$depth][$key]);
            $replace = $choices[mt_rand(0, count($choices) - 1)];

            # Move down to the lower levels
            $level = $depth;
            while($level > 0)
            {
                foreach($store[$level] as $k => $v)
                {
                    $find = $openingConstruct.$store[$depth][$key].$closingConstruct;
                    if($level == 1 AND $depth == 1)
                    {
                        $find = $store[$depth][$key];
                    }
                    $store[$level][$k] = str_replace_first($find, $replace, $store[$level][$k]);
                }
                $level--;
            }
        }
    }

    # Put the very lowest level back into the original string
    foreach($original as $key => $value)
    {
        $string = str_replace_first($openingConstruct.$value.$closingConstruct, $store[1][$key], $string);
    }

    return $string;
}


function smartspin($string, $seedPageName = true, $openingConstruct = '{', $closingConstruct = '}')
{
    # If we have nothing to spin just exit
    if(strpos($string, $openingConstruct) === false)
    {
        return $string;
    }

    # Find all positions of the starting and opening braces
    $startPositions = strpos_all($string, $openingConstruct);
    $endPositions   = strpos_all($string, $closingConstruct);

    # There must be the same number of opening constructs to closing ones
    if($startPositions === false OR count($startPositions) !== count($endPositions))
    {
        return $string;
    }

    # Optional, always show a particular combination on the page
    if($seedPageName)
    {
        mt_srand(crc32($_SERVER['REQUEST_URI']));
    }

    # Might as well calculate these once
    $openingConstructLength = mb_strlen($openingConstruct);
    $closingConstructLength = mb_strlen($closingConstruct);

    # Organise the starting and opening values into a simple array showing orders
    foreach($startPositions as $pos)
    {
        $order[$pos] = 'open';
    }
    foreach($endPositions as $pos)
    {
        $order[$pos] = 'close';
    }
    ksort($order);
    
	
    # Go through the positions to get the depths
    $depth = 0;
    $chunk = 0;
    foreach($order as $position => $state)
    {
        if($state == 'open')
        {
            $depth++;
            $history[] = $position;
        }
        else
        {
            $lastPosition   = end($history);
            $lastKey        = key($history);
            unset($history[$lastKey]);

            $store[$depth][] = mb_substr($string, $lastPosition + $openingConstructLength, $position - $lastPosition - $closingConstructLength);
            $depth--;
        }
    }
    krsort($store);

    # Remove the old array and make sure we know what the original state of the top level spin blocks was
    unset($order);
    $original = $store[1];

    # Move through all elements and spin them
    foreach($store as $depth => $values)
    {
        foreach($values as $key => $spin)
        {
            # Get the choices
            $choices = explode('|', $store[$depth][$key]);
            
			
			$replace = $choices[mt_rand(0, count($choices) - 1)];
            
            # Move down to the lower levels
            $level = $depth;
            while($level > 0)
            {
                foreach($store[$level] as $k => $v)
                {
                    $find = $openingConstruct.$store[$depth][$key].$closingConstruct;
                    
										
					if($level == 1 AND $depth == 1)
                    {
                        $find = $store[$depth][$key];
                    }
                    $store[$level][$k] = str_replace_first($find, $replace, $store[$level][$k]);
                }
                $level--;
            }
        }
    }
    
	//Creating an array
	
	$options = array();
	$count = 1;
	
    # Put the very lowest level back into the original string
    foreach($original as $key => $value)
    {
        $string = str_replace_first($openingConstruct.$value.$closingConstruct, $store[1][$key], $string);
	    
		//echo "
New String is ".$store[1][$key]."
";
	    $options[$count] =     $store[1][$key];
		$count++;
		 
	
    }
	
	//
	//echo "
New String is ".$string."
";
    $options[0] = $string;
	
	return $options;  // Returning Array
	
    //return $string;
}


$title = "{Ways To Get|Learn To Get|Suggestions|Where To Get|Inexpensive|How To Attract|Learn How To Get|Affordable|Obtaining|Ideas|Tips|Tips To Get} More Repeat Customers
Coldness, numbness and loss of hand coordination are ";

$smartSpinContinue = true;

echo "Smart spin check 
";


while($smartSpinContinue == true)
{
	   	$new_spin_title = smartspin($title,false);
		//print_r($new_spin_title);
		
		$new_spin_title[0] = str_replace("|","",$new_spin_title[0]);
		
		//echo $spincount.": ".$new_spin_title[0]."
";
		
		for($i=1; $i<=count($new_spin_title); $i++)
		{
		   //echo $new_spin_title[$i];
		   $title = str_replace($new_spin_title[$i],"",$title);
		   $title = str_replace("{|","{",$title);
		   $title = str_replace("|}","}",$title);
		   $title = str_replace("{}","",$title);
		   $title = str_replace("||","|",$title);
		   
		}
		
    $result = strpos_all($title, "}");
	if($result[0]<1)
	{
	  //Nothing is left that can be spinned so we are exiting 🙂
	  $smartSpinContinue = false;
	  //exit;	
	}
	
	
	//End Smart Spin
	echo $new_spin_title[0]."
";
	
}

Leave a Reply

Your email address will not be published. Required fields are marked *