Skip to main content
Home / Code Snippets / PHP / Append Non-Breaking Space Between Last Two Words

Append Non-Breaking Space Between Last Two Words

<?php

function word_wrapper($text,$minWords = 3) {
       $return = $text;
       $arr = explode(' ',$text);
       if(count($arr) >= $minWords) {
               $arr[count($arr) - 2].= '&nbsp;'.$arr[count($arr) - 1];
               array_pop($arr);
               $return = implode(' ',$arr);
       }
       return $return;
}


?>
icon-link icon-logo-star icon-search icon-star