Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function character_limiter($str, $n = 500, $end_char = '…')
- {
- if (strlen($str) < $n)
- {
- return $str;
- }
- $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
- if (strlen($str) <= $n)
- {
- return $str;
- }
- $out = "";
- foreach (explode(' ', trim($str)) as $val)
- {
- $out .= $val.' ';
- if (strlen($out) >= $n)
- {
- $out = trim($out);
- return (strlen($out) == strlen($str)) ? $out : $out.$end_char;
- }
- }
- }
- $string = 'one two three four';
- $limited = character_limiter($string, 7, '');
- $string = explode($limited, $string);
- $string[0] = $limited;
- print_r($string);
- ?>
- =========================
- Array ( [0] => one two [1] => three four )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement