Last active
August 15, 2016 17:56
-
-
Save bohnna/bd5eca3ddf94b2b66c8c517e6fe52792 to your computer and use it in GitHub Desktop.
Wrap a span around all instances in a string that match a regular expression.
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
| public static function span_strings($string, $class = null) { | |
| /* Takes a string, and surrounds anything surrounded by 4 asterisks with | |
| * a span tag with an optional class name. | |
| * | |
| * If a class is specified in the second argument, it will add | |
| * a class to the span tag. | |
| */ | |
| $match_phrase = "/\*\*([^**]*)\*\*/"; | |
| $replace_string = '<span'; | |
| $replace_string .= $class ? ' class="' . $class . '">' : '>'; | |
| $replace_string .= '${1}</span>'; | |
| $new_string = preg_replace($match_phrase, $replace_string, $string); | |
| return $new_string; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment