Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将wiki格式转换为XHTML_Php_Xhtml_Wiki - Fatal编程技术网

Php 将wiki格式转换为XHTML

Php 将wiki格式转换为XHTML,php,xhtml,wiki,Php,Xhtml,Wiki,我有一个存储“wiki格式”文本的数据库,我想用PHP在XHTML中显示这些文本 以下是包含所有wiki标记的示例输出: Default text == Heading 1 == === Heading 2 === ==== Heading 3 ==== ===== Heading 4 ===== ====== Heading 5 ====== '''Bold''' ''Italic'' <s>Strikethrough</s> * List item 1 * List

我有一个存储“wiki格式”文本的数据库,我想用PHP在XHTML中显示这些文本

以下是包含所有wiki标记的示例输出:

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>
默认文本
==标题1==
==标题2===
==标题3====
======标题4=====
======标题5======
“粗体”
“斜体”
删除线
*清单项目1
*清单项目2
#编号项目1
#编号项目2
[[图片:http://domain.com/image.png|图像名称]]
[http://google.com 链接文本位于此处]
>大宗报价
源代码
这是相当标准的wiki语法吗?有没有一种相当标准的方法可以用PHP来解释它


提前谢谢

这将在很大程度上取决于您用来解析的语言

使用Javascript的客户端


使用ASP或PHP的服务器端?

我想说,当今最标准的类似wiki的格式是。几乎任何平台都有可用的库。

是用PHP编写的,并根据获得许可。因此,您可以使用转换器。

是的,这似乎是一种相当标准的wiki格式。我已经使用PEAR包Text_wiki创建了几个PHP wiki解决方案。它只做您想要的,您甚至可以扩展它以支持任何自定义语法并根据任何规则进行翻译


我想出了一个破解方法,但它在很多方面都有漏洞。这是前进的最佳途径吗

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;
}
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>
<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>
函数wiki2html($text)
{
$text=preg_replace('/source lang=“(.*?”(.*?))(.*?\/source/','$2',$text);
$text=preg_replace('/===(*?)=====/'、'$1'、$text);
$text=preg_replace('/===(*?)=====/'、'$1'、$text);
$text=preg_replace('/==(.*?)=====/'、'$1'、$text);
$text=preg_replace('/==(.*?====/'、'$1'、$text);
$text=preg_replace('/==(.*?==/'、'$1'、$text);
$text=preg_replace(“/”(*?“”/”,“$1”,$text);
$text=preg_replace(“/”(*?“/”),“$1”,$text);
$text=preg_replace('/s(.*?\/s/'、'$1'、$text);
$text=preg\u replace('/\[\[图像:(.*?\\\;(.*?\]\]]/','','',$text);
$text=preg_replace('/\[(.*?(.*?)\]/','','$text);
$text=preg_replace(“/(.*?)\n/”,“$1”,$text);
$text=preg\u replace(“/\*(.*?\n/”,“
  • $1
    • ”,$text); $text=preg_replace(“/
        /”,“$text”); $text=preg#u replace('/#(.*?\n/','
      • $1
      • ',$text); $text=preg_replace(“//”、“$text”); $text=str_replace(“\r\n\r\n”,“

        ”,$text); $text=str_replace(“\r\n”,“
        ,$text”); $text=“”.$text.“

        ”; 返回$text; }
输入:

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;
}
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>
<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>
默认文本
==标题1==
==标题2===
==标题3====
======标题4=====
======标题5======
“粗体”
“斜体”
删除线
*清单项目1
*清单项目2
#编号项目1
#编号项目2
[[图片:http://domain.com/image.png|图像名称]]
[http://google.com 链接文本位于此处]
>大宗报价
源代码
输出:

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;
}
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>
<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>

默认文本
标题1
标题2
标题3
标题4
标题5
粗体
斜体
删除线

  • 清单项目1
  • 清单项目2

  • 编号项目1
  • 编号项目2

  • Blockquote
    源代码


    别忘了foobar标记,我正在使用PHP。我确实把它添加到了标签上,但忘了在问题中提及!降价看起来棒极了!PHP类也是如此。不幸的是,格式与我的字符串不匹配-如果我可以控制数据,我会选择markdown:)markdown不能完成基本操作,您必须下载另一个文件才能完成此操作。仍然在寻找一个基本的包含所有内容的wiki2html convertortext_wiki也很不错!但是我的格式不匹配。我的字符串是非标准的,我想你也可以在文本中添加自己的规则。然后你可以支持任何类型的维基。看起来我的字符串格式不是标准的维基格式,所以我会傲慢地接受我自己的答案。如果有人能改进我的代码,请放心!我已经使用了你的代码的一个简化版本来做一些wikitext解析,最后一段没有用你的版本结束。我使用了一个稍微修改过的结尾:$text=str\u replace(“\r\n\r\n”,“

    ”,$text,$count);如果($count>0)$text.='

    ';添加了水平行
    $text=preg_replace('/---/','
    ',$text)不要忘记清理<代码>$text=preg_replace('%(.*?

    %','$1',$text)
    $text=preg_replace('%(
      *?

    %','$1',$text)
    $text=preg_replace('%(.*?

    %','$1',$text)