Created
March 28, 2016 22:33
-
-
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
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
/** | |
* 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