Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function prevent_long_strings($post, $limit = 3) {
- $word_array = explode(" ", $post);
- $opti_string = "";
- foreach ($word_array as $val) {
- if (preg_match("/(.)\\1{".$limit.",}/", $val)) {
- $char_array = preg_split("//", $val);
- $check = 0;
- for ($i = 0; $i < count($char_array); $i++) {
- if ($char_array[$i] == $char_array[$i-1]) {
- if ($check < $limit - 1) {
- $new_word[] = $char_array[$i];
- }
- $check++;
- } else {
- $new_word[] = $char_array[$i];
- $check = 0;
- }
- }
- $opti_string .= implode("", $new_word)." ";
- unset($new_word);
- } else {
- $opti_string .= $val." ";
- }
- }
- return $opti_string;
- }
- // Example:
- echo prevent_long_strings("Hello, can you heeeeeeeeaaaaaaaar meeeeeeeeeeeee !!!!!!!!!!!!!!!!!!! !!!!!!!!", 3);
- // is converted to "Hello, can you heeeaaar meee !!! !!!"
- ?>
Advertisement
Add Comment
Please, Sign In to add comment