prevent-long-strings
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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 !!! !!!"
?>
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 !!! !!!"
?>