问题描述
我有一个存储wiki 格式"文本的数据库,我想使用 PHP 在 XHTML 中显示该文本.
这是一个包含所有 wiki 标记的示例输出:
默认文本== 标题 1 ===== 标题 2 ======= 标题 3 ========= 标题 4 =========== 标题 5 ======'''胆大'''''斜体''<s>删除线</s>* 列出项目 1* 列出项目 2# 编号项目 1# 编号项目 2[[图片:http://domain.com/image.png|图片名称]][http://google.com 链接文本在此处]>块引用<source lang="language">源代码</source>
这是相当标准的维基语法吗?有没有一种相当标准的方式用 PHP 来解释它?
提前致谢!
我想出了一个 hack,但它破坏了很多东西.这是最好的前进方式吗?
PHP:
function wiki2html($text){$text = preg_replace('/<source lang="(.*?)">(.*?)<\/source>/', '<pre lang="$1">$2</pre>', $text);$text = preg_replace('/======(.*?)======/', '<h5>$1</h5>', $text);$text = preg_replace('/======(.*?)======/', '<h4>$1</h4>', $text);$text = preg_replace('/====(.*?)====/', '<h3>$1</h3>', $text);$text = preg_replace('/===(.*?)===/', '<h2>$1</h2>', $text);$text = preg_replace('/==(.*?)==/', '<h1>$1</h1>', $text);$text = preg_replace("/'''(.*?)'''/", '<strong>$1</strong>', $text);$text = preg_replace("/''(.*?)''/", '<em>$1</em>', $text);$text = preg_replace('/<s>(.*?)<\/s>/', '<strike>$1</strike>', $text);$text = preg_replace('/\[\[Image:(.*?)\|(.*?)\]\]/', '<img src="$1" alt="$2" title="$2"/>', $text);$text = preg_replace('/\[(.*?) (.*?)\]/', '<a href="$1" title="$2">$2</a>', $text);$text = preg_replace('/>(.*?)\n/', '<blockquote>$1</blockquote>', $text);$text = preg_replace('/\* (.*?)\n/', '- $1
', $text);$text = preg_replace('//', '', $text);$text = preg_replace('/# (.*?)\n/', '<ol><li>$1</li></ol>', $text);$text = preg_replace('//', '', $text);$text = str_replace("\r\n\r\n", '</p><p>', $text);$text = str_replace("\r\n", '<br/>', $text);$text = '<p>'.$text.'</p>';返回 $text;}
输入:
默认文本== 标题 1 ===== 标题 2 ======= 标题 3 ========= 标题 4 =========== 标题 5 ======'''胆大'''''斜体''<s>删除线</s>* 列出项目 1* 列出项目 2# 编号项目 1# 编号项目 2[[图片:http://domain.com/image.png|图片名称]][http://google.com 链接文本在此处]>块引用<source lang="language">源代码</source>
输出:
默认文本<br/><h1>标题 1 </h1><br/><h2>标题 2 </h2><br/><h3>标题 3 </h3><br/><h4>标题 4 </h4><br/><h5>标题 5 </h5><br/><strong>粗体</strong><br/><em>斜体</em><br/><strike>删除线</strike></p><p><ul><li>列表项 1</li><li>列表项 2</li><br/><ol><li>编号项目1</li><li>编号项目2</li></ol><br/><img src="http://domain.com/image.png" alt="图片名称" title="图片名称"/></p><p><a href="http://google.com" title="链接文本在此处">链接文本在此处</a></p><p><blockquote>块引用</blockquote><br/><pre lang="language">源代码</pre><br/></p>
I have a database storing "wiki formatted" text which I would like to display in XHTML using PHP.
Here is an example output with all the wiki markup:
Default text
== Heading 1 ==
=== Heading 2 ===
==== Heading 3 ====
===== Heading 4 =====
====== Heading 5 ======
'''Bold'''
''Italic''
<s>Strikethrough</s>
* List item 1
* List item 2
# Numbered item 1
# Numbered item 2
[[Image:http://domain.com/image.png|Image name]]
[http://google.com Link text goes here]
> Blockquote
<source lang="language">Source code</source>
Is that fairly standard wiki syntax? Is there a fairly standard way of interpreting it with PHP?
Thanks in advance!
I came up with a hack, but it breaks on a lot of things. Is this the best way forward?
PHP:
function wiki2html($text)
{
$text = preg_replace('/<source lang="(.*?)">(.*?)<\/source>/', '<pre lang="$1">$2</pre>', $text);
$text = preg_replace('/======(.*?)======/', '<h5>$1</h5>', $text);
$text = preg_replace('/=====(.*?)=====/', '<h4>$1</h4>', $text);
$text = preg_replace('/====(.*?)====/', '<h3>$1</h3>', $text);
$text = preg_replace('/===(.*?)===/', '<h2>$1</h2>', $text);
$text = preg_replace('/==(.*?)==/', '<h1>$1</h1>', $text);
$text = preg_replace("/'''(.*?)'''/", '<strong>$1</strong>', $text);
$text = preg_replace("/''(.*?)''/", '<em>$1</em>', $text);
$text = preg_replace('/<s>(.*?)<\/s>/', '<strike>$1</strike>', $text);
$text = preg_replace('/\[\[Image:(.*?)\|(.*?)\]\]/', '<img src="$1" alt="$2" title="$2" />', $text);
$text = preg_replace('/\[(.*?) (.*?)\]/', '<a href="$1" title="$2">$2</a>', $text);
$text = preg_replace('/>(.*?)\n/', '<blockquote>$1</blockquote>', $text);
$text = preg_replace('/\* (.*?)\n/', '<ul><li>$1</li></ul>', $text);
$text = preg_replace('/<\/ul><ul>/', '', $text);
$text = preg_replace('/# (.*?)\n/', '<ol><li>$1</li></ol>', $text);
$text = preg_replace('/<\/ol><ol>/', '', $text);
$text = str_replace("\r\n\r\n", '</p><p>', $text);
$text = str_replace("\r\n", '<br/>', $text);
$text = '<p>'.$text.'</p>';
return $text;
}
Input:
Default text
== Heading 1 ==
=== Heading 2 ===
==== Heading 3 ====
===== Heading 4 =====
====== Heading 5 ======
'''Bold'''
''Italic''
<s>Strikethrough</s>
* List item 1
* List item 2
# Numbered item 1
# Numbered item 2
[[Image:http://domain.com/image.png|Image name]]
[http://google.com Link text goes here]
> Blockquote
<source lang="language">Source code</source>
Output:
<p>
Default text<br/>
<h1> Heading 1 </h1><br/>
<h2> Heading 2 </h2><br/>
<h3> Heading 3 </h3><br/>
<h4> Heading 4 </h4><br/>
<h5> Heading 5 </h5><br/>
<strong>Bold</strong><br/>
<em>Italic</em><br/>
<strike>Strikethrough</strike>
</p>
<p>
<ul>
<li>List item 1</li>
<li>List item 2</li>
</ul>
<br/>
<ol>
<li>Numbered item 1</li>
<li>Numbered item 2</li>
</ol>
<br/>
<img src="http://domain.com/image.png" alt="Image name" title="Image name" />
</p>
<p>
<a href="http://google.com" title="Link text goes here">Link text goes here</a>
</p>
<p>
<blockquote> Blockquote</blockquote><br/>
<pre lang="language">Source code</pre><br/>
</p>
这篇关于将 wiki 格式转换为 XHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!