Skip to content

Instantly share code, notes, and snippets.

@eriku
Created March 28, 2016 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eriku/5381cae0e5ef9aa39bad to your computer and use it in GitHub Desktop.
Save eriku/5381cae0e5ef9aa39bad to your computer and use it in GitHub Desktop.
Find a word wrapped in an asterisk (*) and wrap it in a span element
/**
* Search for an asterisk (*) in a string and wrap it with an element
*
* @example echo findAndWrap($module->get('title'), 'alternate-text')
*
* @package Erik's Functions
* @subpackage utility
*
* @param string $haystack | String to search
* @param string $class | Class to add to wrapper element
* @param string $wrapper | element type to wrap $needle with
* @param string $needle | What to search for
*
* @todo edit function so developer can specify which $needle to search for
*
*/
function findAndWrap($haystack, $class, $wrapper = 'span', $needle = '*') {
$expression = '/(\*(.*?)\*)/';
if (preg_match($expression, $haystack)){
return preg_replace($expression, '<'.$wrapper.' class="'.$class.'">$2</'.$wrapper.'>', $haystack);
} else {
return $haystack;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment