Skip to content

Instantly share code, notes, and snippets.

@bohnna
Last active August 15, 2016 17:56
Show Gist options
  • Select an option

  • Save bohnna/bd5eca3ddf94b2b66c8c517e6fe52792 to your computer and use it in GitHub Desktop.

Select an option

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.
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