Created
July 18, 2014 18:13
-
-
Save johnbhartley/4a0633391b4f4b9ea930 to your computer and use it in GitHub Desktop.
Span the first word ya dingus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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