Advertisement
Guest User

Untitled

a guest
May 20th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. <p style="padding: 0 5px;font-size: 14px;font-family:monospace;margin-block-start: 0em;margin-block-end: 0em;" >
  2.  
  3. <?php
  4.  
  5. function paint($str,$lenght){ #метод окраски слова в зависимости от длины
  6. $color="#fff";
  7. if($lenght<4){
  8. $color="red";
  9. }
  10. if($lenght>3 && $lenght<8){
  11. $color="orange";
  12. }
  13. if($lenght>7 && $lenght<12){
  14. $color="yellow";
  15. }
  16. if($lenght>11 && $lenght<16){
  17. $color="green";
  18. }
  19. if($lenght>15 && $lenght<20){
  20. $color="aqua";
  21. }
  22. if($lenght>19 && $lenght<24){
  23. $color="blue";
  24. }
  25. if($lenght>23 && $lenght<27){
  26. $color="purple";
  27. }
  28. if($lenght>26){
  29. $color="grey";
  30. }
  31.  
  32. $result = "<span style='color:".$color."'>".$str."</span>"."(".$lenght.")";
  33. return $result;
  34. }
  35.  
  36.  
  37. function replaceS($str,$lenght){ #метод, который заменяет часть элемента спаном с цветом
  38.  
  39. $pattern = "/[\w\s\d]+/u";
  40. preg_match($pattern,$str,$clearStr); #$clearStr[0] - элемент без знаков препинания или варик с #preg_match_all($pattern,$str,$clearStr);, тогда
  41. $result = str_ireplace($clearStr[0], paint($clearStr[0],$lenght), $str); #$result = str_ireplace($clearStr[0], paint($clearStr[0][0] иди [0][1],$lenght), $str);
  42. return $result;
  43. }
  44.  
  45. $text = $_POST["text"];
  46.  
  47. $pattern = "/[\w\s\d]+/u"; #паттерн, который убирает все знаки пунктуации
  48.  
  49. $list = explode (" ", $text);
  50. $resultList = array();
  51.  
  52. foreach ($list as $key){
  53. preg_match($pattern,$key,$res);
  54. $wordLenghtP = iconv_strlen($key); #длина с знаком пунктуации
  55. $wordLenghtW = iconv_strlen($res[0]);#длина без знаком пунктуации
  56.  
  57. $raznica = $wordLenghtP-$wordLenghtW;#количество знаков препинания в элементе
  58.  
  59. if($raznica==0)
  60. array_push($resultList,paint($key,$wordLenghtW));
  61. else{
  62. array_push($resultList,replaceS($key,$wordLenghtW));
  63. }
  64. }
  65. foreach ($resultList as $key) {
  66. echo $key." ";
  67. }
  68. ?>
  69.  
  70. </p>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement