【问题标题】:PHP Simple Wiki Parser requires NOWIKI TagPHP Simple Wiki Parser 需要 NOWIKI 标签
【发布时间】:2011-06-07 21:49:31
【问题描述】:

我有一个简单的函数,可以将纯文本转换为 wiki 标记。但是我遇到了一个问题,我无法阻止某些字符串被修改。所以我需要额外的 nowiki 标记,在其中没有进行任何更改。我对正则表达式不是很熟悉,因此将不胜感激。

<?php
function simpleText($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("/\*\*(.*?)\*\*/", '<b>$1</b>', $text);
 $text = preg_replace("/__(.*?)__/", '<u>$1</u>', $text);
 $text = preg_replace("/\/\/(.*?)\/\//", '<em>$1</em>', $text);
 $text = preg_replace('/\-\-(.*?)\-\-/', '<strike>$1</strike>', $text);
 $text = preg_replace('/\[\[Image:(.*?)\|(.*?)\]\]/', '<img src="$1" alt="$2" title="$2" />', $text);
 $text = preg_replace('/\[(.*?) (.*?)\]/', '<a target="_blank" 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 = '<div class="wikiText">'.$text.'</div>';
 return $text;
}                                                                                          
?>

【问题讨论】:

    标签: php regex function tags wiki


    【解决方案1】:

    你可以使用 (?!ignore)

     $text = preg_replace('(?!--)/======(.*?)======/(?!--)', '<h5>$1</h5>', $text);
    

    如果标头每端都用“--”括起来,则将不对其进行解析。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多