splitSentencesArray($stringToShuffle); $sentences = Array(); $i = 0; $shuffled = ""; // then take each sentance... foreach ($splitSentences as $value) { //trims any whitespace $value = trim($value); //saves sentance deliminater then removes it $totalChars = strlen($value) -1; $sentanceDelim = $value[$totalChars]; $value = str_replace("!", "", $value); $value = str_replace("?", "", $value); $value = str_replace(".", "", $value); // Splits the sentence into words... $words = $this->splitWordsArray($value); // Shuffle each word foreach($words as $value) { // Count how many chars in the word $totalChars = strlen($value) -1; // If there is any - take away punctuation from the end of the word $allowed = "/[^a-z0-9]/i"; $punct =""; $testPunct = $value[$totalChars]; if(preg_replace($allowed,"",$testPunct) =="") { $punct = $value[$totalChars]; $value = str_replace($punct, "", $value); $totalChars = strlen($value) -1; } $firstLetter = ""; $theMiddle = ""; $lastLetter = ""; // Separate the first and last letters $firstLetter = $value[0]; if(strlen($value) > 1) { $lastLetter = $value[$totalChars]; } // Shuffle the middle letters if(strlen($value) > 2) { $theMiddle = str_shuffle(substr($value, 1, -1)); } $shuffled .= " ".$firstLetter.$theMiddle.$lastLetter.$punct; } $shuffled .= $sentanceDelim; } return $shuffled; } /* --------------------------------------------------- */ function shuffleSentences($textToShuffle) { /* --------------------------------------------------- This function shuffles all the sentences from a passed paragraph. --------------------------------------------------- */ // First we separate the paragraphs $textToShuffle = rtrim($textToShuffle); $splitParagraphs = explode("\n\n", $textToShuffle); $paragraphs = Array(); // then take each paragraphs... foreach ($splitParagraphs as $value) { // Split the paragraphs into sentences... $sentences = $this->splitSentencesArray($value); // ...shuffle them shuffle($sentences); // ...and turn them back into paragraphs $newOrderString = implode(" ", $sentences); $paragraphs[$i] = trim($newOrderString)." "; $i = $i + 1; } // Then returns the finished mess return implode("\n\n", $paragraphs); } /* --------------------------------------------------- */ function shuffleWords($textToShuffle) { /* --------------------------------------------------- This function shuffles all the words inside a sentance from a passed string. --------------------------------------------------- */ // First we separate sentences $splitSentences = $this->splitSentencesArray($textToShuffle); $sentences = Array(); $i = 0; // then take each sentance... foreach ($splitSentences as $value) { //trims any whitespace $value = trim($value); //saves sentance deliminater then removes it $totalChars = strlen($value) -1; $sentanceDelim = $value[$totalChars]; $value = str_replace("!", "", $value); $value = str_replace("?", "", $value); $value = str_replace(".", "", $value); // Splits the sentence into words... $value = strtolower($value); $words = $this->splitWordsArray($value); // ...shuffles them shuffle($words); // ...and turns them back into sentences $newOrderString = implode(" ", $words); $sentences[$i] = ucfirst(trim($newOrderString).$sentanceDelim); $i = $i + 1; } // Then returns the finished mess return implode(" ", $sentences); } /* --------------------------------------------------- */ function shiftVowels($textToShift){ /* --------------------------------------------------- This function shifts each vowel so A shifted becomes E --------------------------------------------------- */ $textToShift = str_ireplace("u", ":|1|:", $textToShift); $textToShift = str_ireplace("o", ":|2|:", $textToShift); $textToShift = str_ireplace("i", ":|3|:", $textToShift); $textToShift = str_ireplace("e", ":|4|:", $textToShift); $textToShift = str_ireplace("a", ":|5|:", $textToShift); $textToShift = str_ireplace(":|1|:", "o", $textToShift); $textToShift = str_ireplace(":|2|:", "i", $textToShift); $textToShift = str_ireplace(":|3|:", "e", $textToShift); $textToShift = str_ireplace(":|4|:", "a", $textToShift); $textToShift = str_ireplace(":|5|:", "u", $textToShift); return $textToShift; } /* --------------------------------------------------- */ function removeVowels($textToShift){ /* --------------------------------------------------- This function removes all vowels --------------------------------------------------- */ $textToShift = str_ireplace("a", "", $textToShift); $textToShift = str_ireplace("e", "", $textToShift); $textToShift = str_ireplace("i", "", $textToShift); $textToShift = str_ireplace("o", "", $textToShift); $textToShift = str_ireplace("u", "", $textToShift); return $textToShift; } /* --------------------------------------------------- */ function reverseLetters($textToReverse){ /* --------------------------------------------------- This function reverses the letters in each word keeping punctuation and word order intact --------------------------------------------------- */ $theWord = trim($textToReverse); // Check if the string is a signle word or a sentance if (str_word_count($theWord) > 1) { $words = $this->splitWordsArray($theWord); } else{ $words[0] = $theWord; } $reversed = ""; // Reverse each word foreach($words as $value) { // Count how many chars in the word $totalChars = strlen($value) -1; // If there is any; take away punctuation from the end of the word $allowed = "/[^a-z0-9]/i"; $punct =""; $testPunct = $value[$totalChars]; if(preg_replace($allowed,"",$testPunct) =="") { $punct = $value[$totalChars]; $value = str_replace($punct, "", $value); $totalChars = strlen($value) -1; } // Reverse the word and add back the punctuation if there was any $reversed .= " ".strrev($value).$punct; } // Then return the finished mess return $reversed; } /* --------------------------------------------------- */ function reverseAll($textToReverse) { /* --------------------------------------------------- At the mo just points to strrev - might add some options for punctuation etc later --------------------------------------------------- */ return strrev($textToReverse); } /* --------------------------------------------------- */ function reverseWords($textToReverse, $reverseLetters=FALSE) { /* --------------------------------------------------- This function reverses the word order If an optional second parameter of TRUE is passed when the function is called then the letters in each word are reversed too. --------------------------------------------------- */ // First we split the string into sentances $splitSentences = $this->splitSentencesArray($textToReverse); $sentences = Array(); $i = 0; // then take each sentance... foreach ($splitSentences as $value) { //trim any whitespace $value = trim($value); //save sentance deliminater then remove it $totalChars = strlen($value) -1; $sentanceDelim = $value[$totalChars]; $value = str_replace("!", "", $value); $value = str_replace("?", "", $value); $value = str_replace(".", "", $value); // If $reverseLetters is set to True then reverse the contents of each word also if($reverseLetters==TRUE){ $value = $this->reverseLetters($value); } // Splits the sentence into words... $value = strtolower($value); $words = $this->splitWordsArray($value); // Reverses the order $reversedWords = array_reverse($words); // ...and turns them back into sentences $newOrderString = implode(" ", $reversedWords); // ...adding the punctuation back on $sentences[$i] = ucfirst(trim($newOrderString).$sentanceDelim); $i++; } // Then returns the finished mess return implode(" ", $sentences); } /* --------------------------------------------------- */ function randomCaps($textToCaps, $percentUpper=65) { /* --------------------------------------------------- This function randomly changes the capitalisation !! Only works with PHP5 --------------------------------------------------- */ // Turn the string into an array $charArray = str_split($textToCaps); $charNum = count($charArray); // Loop through the charecters $i=0; while($i <= $charNum) { // Give upper or lowercase depending on if it is an even number if(rand(0, 100) <= $percentUpper) { $charArray[$i] = strtolower($charArray[$i]); } else { $charArray[$i] = strtoupper($charArray[$i]); } $i++; } return implode($charArray); } /* --------------------------------------------------- */ function alternatingCaps($textToCaps) { /* --------------------------------------------------- This function alternats the capitalisation of each char starting with Upercase !! Only works with PHP5 --------------------------------------------------- */ // Turn the string into an array $charArray = str_split($textToCaps); $charNum = count($charArray); // Loop through the charecters $i=0; while($i <= $charNum) { // Give upper or lowercase randomly if($i&1) { $charArray[$i] = strtolower($charArray[$i]); } else { $charArray[$i] = strtoupper($charArray[$i]); } $i++; } return implode($charArray); } /* --------------------------------------------------- */ function sortWordsAlphabetically($textToSort, $sentancesSeparate=TRUE, $reverseAplha=FALSE) { /* --------------------------------------------------- This function sorts the words alphabetically The two optional parameters decide if the words in each sentance should be sorted separately and if they should be sorted in reverse order. --------------------------------------------------- */ if($sentancesSeparate==FALSE) { $splitSentences[0] = $textToSort; } else { // We split the string into sentances $splitSentences = $this->splitSentencesArray($textToSort); } $i = 0; $sentences = Array(); // then take each sentance... foreach ($splitSentences as $value) { //trim any whitespace $value = trim($value); //save sentance deliminater then remove it $totalChars = strlen($value) -1; $sentanceDelim = $value[$totalChars]; $value = str_replace("!", "", $value); $value = str_replace("?", "", $value); $value = str_replace(".", "", $value); $value = str_replace("[", "[ ", $value); $value = str_replace("]", " ]", $value); $value = str_replace("(", "( ", $value); $value = str_replace(")", " )", $value); $value = str_replace("'", " ' ", $value); // Splits the sentence into words... $value = strtolower($value); $words = $this->splitWordsArray($value); // If $reverseAplha is set to True then reverse the order if($reverseAplha==TRUE) { rsort($words); } else { // Otherwise sort the words Alphabeticaly sort($words); } // ...and turns them back into sentences $newOrderString = implode(" ", $words); // ...adding the punctuation back on $sentences[$i] = ucfirst(trim($newOrderString).$sentanceDelim); $i = $i + 1; } // Then returns the finished mess return implode(" ", $sentences); } /* --------------------------------------------------- */ function reverseQuestionExclamtion($textToChange) { $textToChange = str_replace("!", "||:!:||", $textToChange); $textToChange = str_replace("?", "||:?:||", $textToChange); $textToChange = str_replace("||:!:||", "?", $textToChange); $textToChange = str_replace("||:?:||", "!", $textToChange); return $textToChange; } /* --------------------------------------------------- */ /* --------------------------------------------------- */ } /* Jedidiah Broadbent June 2008 CC : by-nc-sa http://creativecommons.org/licenses/by-nc-sa/3.0/ */