I need to take an input from a WYSIWYG and break it down to having 33 (or approximate) characters per line. If it hits the limit, I need to insert a new line. Here is what I came up with:
for($i=0; $i < strlen($string); $i++) {
if ($i % 33 == 0 && $i != 0) {
$characters[] = '\r';
}
$characters[] = $string[$i];
}
$result = implode($characters);
This breaks HTML tags and words. I need to ignore HTML tags, and allow words to finish before breaking a line. What is a better way to achieve this?
What I would do is explode by space, then figure out the number of “words” that can fit on a line without going over 33 characters.
I just did this in here and haven’t tested it, but it’s the basic general idea. Hope it helps. Good luck. 🙂
Edit: added strip_tags() to the length check so tags won’t affect the length of the line (since they’re invisible anyway)