Skip to content

Instantly share code, notes, and snippets.

@johnbhartley
Created July 18, 2014 18:13
Show Gist options
  • Save johnbhartley/4a0633391b4f4b9ea930 to your computer and use it in GitHub Desktop.
Save johnbhartley/4a0633391b4f4b9ea930 to your computer and use it in GitHub Desktop.
Span the first word ya dingus
$string = 'Test me more';
$pattern = '/^(\S+)/';
$replacement = '<span class="first-word">$1</span>';
echo preg_replace($pattern, $replacement, $string);
// or
echo preg_replace('/^(\S+)/', '<span class="first-word">$1</span>', 'Test me more');
// from @greg5green
/^(\S+)/ = at the start of the string, match between 1 and infinite number of characters that are not whitespace characters
^ = start of string
\S = not whitespace characters
+ = 1 to infinity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment