irwan

Prevent the post of long strings from comment user

Nov 15th, 2011
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.07 KB | None | 0 0
  1. <?php
  2. function prevent_long_strings($post, $limit = 3) {
  3.     $word_array = explode(" ", $post);
  4.     $opti_string = "";
  5.     foreach ($word_array as $val) {
  6.         if (preg_match("/(.)\\1{".$limit.",}/", $val)) {
  7.             $char_array = preg_split("//", $val);
  8.             $check = 0;
  9.             for ($i = 0; $i < count($char_array); $i++) {
  10.                 if ($char_array[$i] == $char_array[$i-1]) {
  11.                     if ($check < $limit - 1) {
  12.                         $new_word[] = $char_array[$i];
  13.                     }
  14.                     $check++;
  15.                 } else {
  16.                     $new_word[] = $char_array[$i];
  17.                     $check = 0;
  18.                 }
  19.             }
  20.             $opti_string .= implode("", $new_word)." ";
  21.             unset($new_word);
  22.         } else {
  23.             $opti_string .= $val." ";
  24.         }
  25.     }
  26.     return $opti_string;
  27. }
  28. // Example:
  29. echo prevent_long_strings("Hello, can you heeeeeeeeaaaaaaaar meeeeeeeeeeeee !!!!!!!!!!!!!!!!!!! !!!!!!!!", 3);
  30. // is converted to "Hello, can you heeeaaar meee !!! !!!"
  31. ?>
  32.  
Advertisement
Add Comment
Please, Sign In to add comment