I am trying to write a php script for my site that will allow me to use spin syntax to inject words (pre defined) into a paragraph. I am just not sure how to do it multiple times in one script. For instance, I have a paragraph like this:
The {fat|pudgy|lazy} dog {sleeps|rest|poops} all day long.
I am trying to make the script access each group of text in between the {text 1|text 2} curly brackets and then randomly choose which variable to use (separated by pipes). When it is done it will spit out many variations of the string such as:
- The fat dog sleeps all day long.
- The lazy dog poops all day long.
Etc.
I am able to access the first instance of the text with in the {} brackets and then spin that, but I just dont know how to do it multiple times in one fell swoop. Anyone ever done this?
Here is my script to access the first instance of text in between the first two {} brackets.
function get_between ($text, $s1, $s2) {
$spinText = "";
$pos_s = strpos($text,$s1);
$pos_e = strpos($text,$s2);
for ( $i=$pos_s+strlen($s1) ; (( $i<($pos_e)) && $i < strlen($text)) ; $i++ ) {
$spinText .= $text[$i];
}
return $spinText;
}
$str = "The {fat|pudgy|lazy} dog {sleeps|rest|poops} all day long.";
$spinTextFinal = get_between($str,"{","}");
$spinTextFinalExplode = explode("|",$spinTextFinal);
print_r($spinTextFinalExplode);
Figured it out 🙂