Created
February 20, 2012 15:13
-
-
Save ReneeVandervelde/1869616 to your computer and use it in GitHub Desktop.
MarkDown
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
<? | |
function markDown($str){ | |
//Tag like markdown, ordered by precedence. | |
//This is the only thing you really need to configure. | |
//It's a regular expression (without delimiters) as the key and the desired output as the value. | |
$markdownSet = array( | |
"\_\*(.*)\*_" => " <b><i>$2</i></b> ", | |
"\*_(.*)_\*" => " <b><i>$2</i></b> ", | |
"_(.*)_" => " <i>$2</i> ", | |
"\*(.*)\*" => " <b>$2</b> ", | |
"\#\#\#(.*)\#\#\#" => " <h3>$2</h3> ", | |
"\#\#(.*)\#\#" => " <h2>$2</h2> ", | |
"\#(.*)\#" => " <h1>$2</h1> " | |
); | |
//Strip out the HTML tags, replace them with text | |
$str = preg_replace('/</','<',$str); | |
$str = preg_replace('/>/','>',$str); | |
//Just to make sure they're all gone. | |
$str = strip_tags($str); | |
foreach($markdownSet as $match => $result){ | |
$str = preg_replace("/([[:space:]]|\n|^)".$match."([[:space:]]|\n|$)/U","$1".$result."$3",$str); | |
} | |
//Make links into links | |
$str = preg_replace("/http([s]?):\/\/([^\ \)\n$]*)/i","<a href='\\0' rel='nofollow'>\\0</a>",$str); | |
//Make Newlines into breaks | |
$str = nl2br($str); | |
return $str; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment