Skip to content

Instantly share code, notes, and snippets.

@elron
Created May 23, 2020 16:25
Show Gist options
  • Save elron/dc8a1e98c1bb047b65a2656cf0a9b20d to your computer and use it in GitHub Desktop.
Save elron/dc8a1e98c1bb047b65a2656cf0a9b20d to your computer and use it in GitHub Desktop.
<?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