The E-Hustle
Newbie
- Dec 10, 2009
- 42
- 19
OK, NEED ASAP
I think this will be a simple task for most .php programmers. not sure.. We have Wordpress plugin that we built and what it does is take spun content like this - {word1|word2|word3} and it also replaces the word CITYNAME or THEKEYWORD with either a list, or our predetermined cities and states. and it automatically makes pages or posts which ever you choose.
Now all we need worked on is the spinner code the rest is fine. It works perfectly right now except we want to be able nest spins inside of spins as much as we like just like "The Best Spinner" does, so we know it can be done. So again we can do {word1|word2|word3} now, but we want to be able to do {{word1|word2|word3}|{word1|word2|word3}|{word1|word2|word3}} or even {{{word1|word2|word3}|{word1|word2|word3}|{word1|word2|word3}}|{{word1|word2|word3}|{word1|word2|word3}|{word1|word2|word3}}}
P.S. it also has a problem with apostrophes too, if we use (don't. you're, or won't) it starts putting a bunch of these //// in there, so if you can fix that too, that would be great....
I am going to put the .php code here below that we are using,
CURRENT .PHP FILE BELOW
<?php
class
TextSpinner
{
private $_input = '';
private $_textOptions = array();
public function __construct(
$input = '')
{
$this->setInput($input);
}
/******************************************************************
* getters:
*/
public function
getInput()
{
return($this->_input);
}
protected function
getTextOptions()
{
return($this->_textOptions);
}
/******************************************************************
* setters:
*/
public function
setInput(
$input)
{
$this->_input = $input;
}
protected function
setTextOptions(
array $textOptions)
{
$this->_textOptions =
(is_array($textOptions) ? $textOptions : array());
}
/******************************************************************
* public methods:
*/
public function
getRandom(
$limit = 1)
{
$output = array();
for($i = 0; $i < $limit; $i++)
{
$output[] = $this->_processInputRandom();
}
return($output);
}
/******************************************************************
* private methods:
*/
private function
_processInputRandom()
{
/*
* Expected syntax:
* This is a {option 1[, option 2[, ...]]}
*/
$data = $this->getInput();
/*
* Send everything surrounded by { }s to $this->_processSegment
* _processSegment will replace {...} with {#} where # = the position
* of this set within the input, i.e. the occurance of {...} will be
* replaced with {0}, then {1}, etc.
*/
$data = preg_replace_callback('/{([^}]+)}/',
array(&$this, '_processSegment'),
$data);
/*
* Send everything surrounded by { }s to $this->_processReplacement
* _processReplacement will replace {#} with a random item from the
* replacement set at this the index indicated by #.
*/
$data = preg_replace_callback('/{([^}]+)}/',
array(&$this, '_processReplacementRandom'),
$data);
return($data);
}
// preg_replace callback function
private function
_processReplacementRandom(
array $input)
{
$options = $this->getTextOptions();
$count = count($options);
if (count($input) > 0)
{
$data = $input[1];
$data = preg_replace('[^0-9]', '', $data);
$index = (int)$data;
if ($index < $count)
{
$max = count($options[$index]) - 1;
$random = rand(0, $max);
return($options[$index][$random]);
}
}
return('{' . $count . '}');
}
// preg_replace callback function
private function
_processSegment(
array $input)
{
$options = $this->getTextOptions();
$count = count($options);
if (count($input) > 0)
{
$data = $input[1];
$substitutions = split("\|", $data);
foreach($substitutions as $substitution)
{
$options[$count][] = $substitution;
}
}
$this->setTextOptions($options);
return('{' . $count . '}');
}
}
/*
* Example
*
*
* $input = <<<EOB
* Never gonna {give you up,let you down,run around and desert you}<br />
* Never gonna {make you cry,say goodbye,tell a lie and hurt you}.
* EOB;
*
* $spinner = new TextSpinner($input);
* $output = $spinner->getRandom(2);
* print(implode("<br />\n", $output));
*
*/
?>
I think this will be a simple task for most .php programmers. not sure.. We have Wordpress plugin that we built and what it does is take spun content like this - {word1|word2|word3} and it also replaces the word CITYNAME or THEKEYWORD with either a list, or our predetermined cities and states. and it automatically makes pages or posts which ever you choose.
Now all we need worked on is the spinner code the rest is fine. It works perfectly right now except we want to be able nest spins inside of spins as much as we like just like "The Best Spinner" does, so we know it can be done. So again we can do {word1|word2|word3} now, but we want to be able to do {{word1|word2|word3}|{word1|word2|word3}|{word1|word2|word3}} or even {{{word1|word2|word3}|{word1|word2|word3}|{word1|word2|word3}}|{{word1|word2|word3}|{word1|word2|word3}|{word1|word2|word3}}}
P.S. it also has a problem with apostrophes too, if we use (don't. you're, or won't) it starts putting a bunch of these //// in there, so if you can fix that too, that would be great....
I am going to put the .php code here below that we are using,
CURRENT .PHP FILE BELOW
<?php
class
TextSpinner
{
private $_input = '';
private $_textOptions = array();
public function __construct(
$input = '')
{
$this->setInput($input);
}
/******************************************************************
* getters:
*/
public function
getInput()
{
return($this->_input);
}
protected function
getTextOptions()
{
return($this->_textOptions);
}
/******************************************************************
* setters:
*/
public function
setInput(
$input)
{
$this->_input = $input;
}
protected function
setTextOptions(
array $textOptions)
{
$this->_textOptions =
(is_array($textOptions) ? $textOptions : array());
}
/******************************************************************
* public methods:
*/
public function
getRandom(
$limit = 1)
{
$output = array();
for($i = 0; $i < $limit; $i++)
{
$output[] = $this->_processInputRandom();
}
return($output);
}
/******************************************************************
* private methods:
*/
private function
_processInputRandom()
{
/*
* Expected syntax:
* This is a {option 1[, option 2[, ...]]}
*/
$data = $this->getInput();
/*
* Send everything surrounded by { }s to $this->_processSegment
* _processSegment will replace {...} with {#} where # = the position
* of this set within the input, i.e. the occurance of {...} will be
* replaced with {0}, then {1}, etc.
*/
$data = preg_replace_callback('/{([^}]+)}/',
array(&$this, '_processSegment'),
$data);
/*
* Send everything surrounded by { }s to $this->_processReplacement
* _processReplacement will replace {#} with a random item from the
* replacement set at this the index indicated by #.
*/
$data = preg_replace_callback('/{([^}]+)}/',
array(&$this, '_processReplacementRandom'),
$data);
return($data);
}
// preg_replace callback function
private function
_processReplacementRandom(
array $input)
{
$options = $this->getTextOptions();
$count = count($options);
if (count($input) > 0)
{
$data = $input[1];
$data = preg_replace('[^0-9]', '', $data);
$index = (int)$data;
if ($index < $count)
{
$max = count($options[$index]) - 1;
$random = rand(0, $max);
return($options[$index][$random]);
}
}
return('{' . $count . '}');
}
// preg_replace callback function
private function
_processSegment(
array $input)
{
$options = $this->getTextOptions();
$count = count($options);
if (count($input) > 0)
{
$data = $input[1];
$substitutions = split("\|", $data);
foreach($substitutions as $substitution)
{
$options[$count][] = $substitution;
}
}
$this->setTextOptions($options);
return('{' . $count . '}');
}
}
/*
* Example
*
*
* $input = <<<EOB
* Never gonna {give you up,let you down,run around and desert you}<br />
* Never gonna {make you cry,say goodbye,tell a lie and hurt you}.
* EOB;
*
* $spinner = new TextSpinner($input);
* $output = $spinner->getRandom(2);
* print(implode("<br />\n", $output));
*
*/
?>