问题
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!
回答1:
I would say that the most standard wiki-like format around today is markdown. There are libraries available for almost any platform, including PHP.
回答2:
MediaWiki is written in PHP and licensed under GPL. So you just could take the WikiText converter and use it.
回答3:
Yes, that seems like a fairly standard wiki format. I've created several PHP wiki solutions using the PEAR package Text_Wiki. It does just what you want, and you can even expand it to support any custom syntax and translate according to any rule.
http://pear.php.net/package/Text_Wiki
回答4:
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>
回答5:
It's going to depend very much on what language you are using to parse.
Client-side with Javascript?
Server Side with ASP or PHP?
来源:https://stackoverflow.com/questions/1252118/converting-wiki-format-to-xhtml