Created
May 23, 2020 16:25
-
-
Save elron/dc8a1e98c1bb047b65a2656cf0a9b20d to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
/** | |
* Wrap last word with span | |
* @author: Elron | |
* https://stackoverflow.com/questions/18612872/get-the-last-word-of-a-string | |
*/ | |
function wrap_last_word($string) { | |
// Breaks string to pieces | |
$pieces = explode(" ", $string); | |
// Modifies the last word | |
$pieces[count($pieces)-1] = '<span class="is-last-word">' . $pieces[count($pieces)-1] . '</span>'; | |
// Returns the glued pieces | |
return implode(" ", $pieces); | |
} | |
wrap_last_word('hello this is wrapped'); | |
// returns this: | |
// hello this is <span class="is-last-word">wrapped</span> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment