Skip to content

Instantly share code, notes, and snippets.

@interactiveRob
Last active November 19, 2018 17:45
Show Gist options
  • Select an option

  • Save interactiveRob/eb69c4ca49a20f9e2d45abc0826febe2 to your computer and use it in GitHub Desktop.

Select an option

Save interactiveRob/eb69c4ca49a20f9e2d45abc0826febe2 to your computer and use it in GitHub Desktop.
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment