2011-01-12 58 views
3

我有一個簡單的函數可以將純文本轉換爲wiki標記。但是我遇到了一個問題,我無法防止某些字符串被修改。所以我需要額外的nowiki標記,其中不做任何更改。我不是很熟悉正則表達式,所以幫助將不勝感激。PHP簡單的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; 
}                       
?> 

回答

0

您可以使用

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

將從正在分析中排除的頭,如果它與封閉的(忽略?!) - 每端'。