Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $text = trim($_GET['text']);
- $arrayOfWords = [];
- $arrayOfWords = preg_split('/[^a-zA-Z]/', $text, -1, PREG_SPLIT_NO_EMPTY);
- $index = 0;
- $indexOfCurrentWord = 0;
- for($i = 0; $i < count($arrayOfWords); $i++) {
- $tempIndex = stripos($text, $arrayOfWords[$i], $index);
- $currentWord = $arrayOfWords[$i];
- if(preg_match('/^[A-Z0-9\s]*$/', $currentWord)) {
- $currentMatch = strrev($currentWord);
- if($currentMatch === $currentWord) {
- $arrCurrentMatch = str_split($currentMatch);
- $currentMatch = '';
- foreach ($arrCurrentMatch as $ch) {
- $currentMatch .= $ch . $ch;
- }
- }
- $lenOfCurrentWord = strlen($currentMatch);
- $index = $tempIndex + strlen($currentWord);
- if(strlen($currentMatch) !== strlen($currentWord)) {
- $arrText = str_split($text);
- for($j = 0; $j < strlen($currentWord); $j++) {
- array_splice($arrText, $index, 0, " ");
- }
- $arrText = join('',$arrText);
- $text = $arrText;
- }
- $text = substr_replace($text, $currentMatch, $tempIndex, $lenOfCurrentWord);
- }
- $index = $tempIndex + strlen($currentWord);
- }
- echo "<p>".htmlspecialchars($text)."</p>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement