tags:

views:

364

answers:

5

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!

A: 

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?

Chris Sobolewski
I'm using PHP. I did add it to the tags but forgot to mention in the question!
Al
+2  A: 

I would say that the most standard wiki-like format around today is markdown. There are libraries available for almost any platform, including PHP.

troelskn
markdown looks fantastic! As does the PHP class. Unfortunately the formatting doesn't match my string - if I had control over the data I would choose markdown :)
Al
+1  A: 

MediaWiki is written in PHP and licensed under GPL. So you just could take the WikiText converter and use it.

Gumbo
+1  A: 

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

danglund
text_wiki also looks great! But my formatting doesn't match that. My string is non-standard I suspect
Al
You can add your own rules to text_wiki too. Then you can support any typ of Wiki.
danglund
A: 

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('/&lt;source lang=&quot;(.*?)&quot;&gt;(.*?)&lt;\/source&gt;/', '<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('/&lt;s&gt;(.*?)&lt;\/s&gt;/', '<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('/&gt;(.*?)\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>
Al
It looks like my string format isn't *standard* wikiformat so I'm going to arrogantly accept my own answer. If anyone can improve my code please feel free!
Al
I've used a cut down verson of your code to do some wikitext parsing, the last paragraph isn't closed with your version. I used a slightly modified end:$text = str_replace("\r\n\r\n", '</p><p>', $text,$count);if($count>0) $text .= '</p>';
Duncan