wikipoints/ground/classes/Markdown.php

71 lines
1.6 KiB
PHP

<?php
class Markdown
{
static function processing($string)
{
$string = self::replaceHeaders($string);
$string = self::replaceBoldItalic($string);
$string = self::replaceBold($string);
$string = self::replaceItalic($string);
$string = self::replaceUnderline($string);
$string = self::replaceUrls($string);
return $string;
}
static private function replaceHeaders($string)
{
for ($index = 3; $index > 0; $index--) {
$pr = '/(^#{' . $index . '} .{1,})/m';
preg_match_all($pr, $string, $headers);
if (isset($headers[0][0])) {
foreach ($headers[0] as $header) {
$hI = $index+1;
$string = str_replace($header, "<h$hI>" . mb_substr($header, $index + 1, NULL, 'UTF-8') . "</h$hI>", $string);
}
}
}
return $string;
}
static private function replaceBoldItalic($string)
{
$pr = '(\*{3}(.+?)\*{3})';
$rep = '<b><i>$1</i></b>';
return preg_replace($pr, $rep, $string);
}
static private function replaceBold($string)
{
$pr = '(\*{2}(.+?)\*{2})';
$rep = '<b>$1</b>';
return preg_replace($pr, $rep, $string);
}
static private function replaceItalic($string)
{
$pr = '(\*{1}(.+?)\*{1})';
$rep = '<i>$1</i>';
return preg_replace($pr, $rep, $string);
}
static private function replaceUnderline($string)
{
$pr = '(\_{1}(.+?)\_{1})';
$rep = '<u>$1</u>';
return preg_replace($pr, $rep, $string);
}
static private function replaceUrls($string)
{
$pr = '((https{0,1}:\/\/[\da-z\.-]+\.[a-z]{2,6})[\/\w\?=&#%+\.,-]*\/?)';
$rep = '<a href="$0" target="_blank">$1</a>';
return preg_replace($pr, $rep, $string);
}
}