Tag: PHP wiki

preg_matchだけでwikiに挑戦

サンプル

ソース

<?php
$s = <<<EOSEOS;

$def = array(
"/\r?\n/"                                               => "\n",
                                                         
"/\n(LEFT|CENTER|RIGHT):([^\n]+)/e"                     => "'\n<div style=\"text-align:' . strtolower('\\1') . '\">\\2</div>'",
"/\n#br\n/"                                             => "\n<br />\n",
"/\n(#hr|-{4,})\n/"                                     => "\n<hr />\n",
"/~\n/"                                                 => "<br />",
"!(https?://[\w\d\./\-\_\?\&\%]+)!e"                    => "'<a href=\"\\1\">' . (preg_match('/(gif|jpe?g|png)$/', '\\1') ? '<img alt=\"\\1\" src=\"\\1\" />' : '\\1') . '</a>'",
"/([\w\d\.\_\-]+@[\w\d\.\-]+)/"                         => "<a href=\"mailto:\\1\">\\1</a>",
"/\n([^ \n].*)(&date;|date\?)/e"                        => "'\n\\1' . date('Y-m-d')",
"/\n([^ \n].*)(&time;|time\?)/e"                        => "'\n\\1' . date('H:i:s')",
"/\n([^ \n].*)(&now;|now\?)/e"                          => "'\n\\1' . date('Y-m-d (D) H:i:s')",

"/\n((?:\|[^\n]+)(?:\n\|[^\n]+)*)/e"                    => "'\n<table class=\"style_table\">\n<' . (strpos('\\1', \"|h\n\") === false ? 'tbody' : 'thead') . '>\n\\1\n</tbody>\n</table>\n'",
"/\n(?:\|([^\n\|]+(?:\|[^\n\|]+)*)\|)/e"                => "'\n<tr><td class=\"style_td\">' . preg_replace('/\|/', '</td><td>', '\\1') . '</td></tr>'",
"/tr>h\n/"                                              => "tr>\n</thead>\n<tbody>\n",
                                                         
"/\n((?:(?:[^\-\+<> \*\n]|\~)[^\n]+\n)+)/"              => "\n<p>\n\\1</p>\n",
"/\n((?:([>])[^\n]+\n)(?:[\-\+>][^\n]+\n)*)/"           => "\n<ul\\2>\n\\1</ul\\2>\n",
"/\n((?:([>]{2})[^\n]+\n)(?:[\-\+>]{2}[^\n]+\n)*)/"     => "\n<ul\\2>\n\\1</ul\\2>\n",
"/\n((?:([>]{3})[^\n]+\n)(?:[\-\+>]{3}[^\n]+\n)*)/"     => "\n<ul\\2>\n\\1</ul\\2>\n",
"/\n((?:([\-\+])[^\n]+\n)(?:[\-\+][^\n]+\n)*)/"         => "\n<ul\\2>\n\\1</ul\\2>\n",
"/\n((?:([\-\+]{2})[^\n]+\n)(?:[\-\+]{2}[^\n]+\n)*)/"   => "\n<ul\\2>\n\\1</ul\\2>\n",
"/\n((?:([\-\+]{3})[^\n]+\n)(?:[\-\+]{3}[^\n]+\n)*)/"   => "\n<ul\\2>\n\\1</ul\\2>\n",
"/\n((?: [^\n]+\n)+)/"                                  => "\n<pre>\n\\1</pre>\n",
                                                         
"/&br;/"                                                => "\n<br />\n",
"/'''([^']+)'''/"                                       => "<em>\\1</em>",
"/''([^']+)''/"                                         => "<strong>\\1</strong>",
"/%%([^']+)%%/"                                         => "<del>\\1</del>",
"/&color\(([^,\)]+),?([^\)]+|)\){([^}]+)};/e"           => "'<span style=\"color:\\1' . (strlen('\\2') > 0 ? ';background-color:\\2' : '') . '\">\\3</span>'",
"/COLOR\(([^\)]+)\){([^}]+)}/"                          => "<span style=\"color:\\1\">\\2</span>",
"/&size\(([^\)]+)\){([^}]+)};/"                         => "<span style=\"font-size:\\1px\">\\2</span>",
"/SIZE\(([^\)]+)\){([^}]+)}/"                           => "<span style=\"font-size:\\1px\">\\2</span>",
                                                         
"/\n\~([^\n]+)/"                                        => "\n\\1",
"/\n[\-\+]{1,3}([^\n]+)/"                               => "\n<li>\\1</li>",
"/\n>{1,3}([^\n]+)/"                                    => "\n<p class=\"quotation\">\\1</p>",
"/\n {1,3}([^\n]+)/"                                    => "\n\\1",
"/\n(\*{1,3})([^\n]+)/e"                                => "'\n<h' . (strlen('\\1') + 1) . '>\\2</h' . (strlen('\\1') + 1) . '>'",
                                                         
"/<(\/?)ul\-{1,3}>/"                                    => "<\\1ul>",
"/<(\/?)ul\+{1,3}>/"                                    => "<\\1ol>",
"/<(\/?)ul\>{1,3}>/"                                    => "<\\1blockquote>",
);

$p = array_keys($def);
$r = array_values($def);
$s1 = preg_replace($p, $r, $s);

echo <<< EOS
<?xml version="1.0" encoding="euc-jp"?>
EOS;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<meta http-equiv="Content-Language" content="ja" />
<meta http-equiv="Content-Type" content="text/html; charset=euc-jp" /><title>php</title>
<link rel="stylesheet" type="text/css" media="screen" href="/skin/pukiwiki.css.php?charset=Shift_JIS" charset="Shift_JIS" />
</head>
<body>
<?php echo $s1; ?>
</body>
</html>

Last-modified: 2010-05-25 (火) 16:24:04 (2759d)