interactiveRob
11/19/2018 - 5:42 PM

Regex Replace to break any name into two lines

Regex Replace to break any name into two lines

/* Two step Regex replace to make sure names always break into 2 lines:
    1) Remove all spaces entered by Human error from the end of the Full Name 
    2) Replace spaces with <br/> tags. Definitions
        (?!^\s) Don't match spaces that come at the beginning of the name
        (?!\w+\s) Also don't match a space after the First Name if there is another 
        word followed by a space after that. This prevents breaking the line for Middle Names.
*/
$title = preg_replace('/\s+$/', '', $title); 
$title = preg_replace('/(?!^\s)\s(?!\w+\s)/', '<br/>', $title);