Advertisement
zornitza_gencheva

02_Uppercase_Words_examTask

Sep 2nd, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. <?php
  2. $text = trim($_GET['text']);
  3.  
  4. $arrayOfWords = [];
  5. $arrayOfWords = preg_split('/[^a-zA-Z]/', $text, -1, PREG_SPLIT_NO_EMPTY);
  6.  
  7. $index = 0;
  8. $indexOfCurrentWord = 0;
  9.  
  10. for($i = 0; $i < count($arrayOfWords); $i++) {
  11. $tempIndex = stripos($text, $arrayOfWords[$i], $index);
  12. $currentWord = $arrayOfWords[$i];
  13.  
  14. if(preg_match('/^[A-Z0-9\s]*$/', $currentWord)) {
  15. $currentMatch = strrev($currentWord);
  16. if($currentMatch === $currentWord) {
  17. $arrCurrentMatch = str_split($currentMatch);
  18. $currentMatch = '';
  19. foreach ($arrCurrentMatch as $ch) {
  20. $currentMatch .= $ch . $ch;
  21. }
  22. }
  23. $lenOfCurrentWord = strlen($currentMatch);
  24. $index = $tempIndex + strlen($currentWord);
  25.  
  26. if(strlen($currentMatch) !== strlen($currentWord)) {
  27. $arrText = str_split($text);
  28. for($j = 0; $j < strlen($currentWord); $j++) {
  29. array_splice($arrText, $index, 0, " ");
  30. }
  31. $arrText = join('',$arrText);
  32. $text = $arrText;
  33. }
  34. $text = substr_replace($text, $currentMatch, $tempIndex, $lenOfCurrentWord);
  35. }
  36. $index = $tempIndex + strlen($currentWord);
  37. }
  38. echo "<p>".htmlspecialchars($text)."</p>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement