I need to limit this string to max 320 characters but also make it truncate cleanly if it is going to overrun e.g
I want
Testing Item 1 | Testing Item 2 | Testing Item 3
Instead of
Testing Item 1 | Testing Item 2 | Testing Item 3 | Tes
Currently I have this function:
/**
* First filter out any empty values, strip any tags, then join each array item with a pipe | and limit it to 320 characters.
* @param $array
* @return string
*/
public function limitStripAndJoin($array) {
// Removes any empty strings from the array e.g if there's no caption on an image, we want to remove the empty string
$filteredArray = array_filter($array, 'strlen');
return strip_tags(substr(join(" | ", $filteredArray), 0, 320));
}
I just need to know how stop going to the next item if it will overrun?