What are some alternative solutions to using wordwrap in PHP to display smilies correctly in consecutive sequences?

When using wordwrap in PHP to display text containing smilies in consecutive sequences, the function may break the sequences and display them incorrectly. One solution is to use a regular expression to identify and preserve the smilies before applying wordwrap. This way, the smilies will remain intact and displayed correctly in the text.

$text = "Hello :) How are you? I'm feeling great! :)";
$smilies = array(':)', ':(');

// Preserve smilies before applying wordwrap
foreach ($smilies as $smiley) {
    $text = preg_replace('/' . preg_quote($smiley) . '/', str_replace(':', ':', $smiley), $text);
}

// Apply wordwrap to the text
$wrappedText = wordwrap($text, 20, "\n", true);

// Restore smilies after wordwrap
foreach ($smilies as $smiley) {
    $wrappedText = str_replace(str_replace(':', ':', $smiley), $smiley, $wrappedText);
}

echo $wrappedText;