PHP Simple Wiki Parser requires a NOWIKI tag

I have a simple function that converts plain text to wiki markup. But I ran into a problem when I cannot prevent some lines from changing. Therefore, I need an additional nowiki tag in which no changes have been made. I am not very familiar with regex, so help would be appreciated.

<?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;
}                                                                                          
?>
+3
source share
1 answer

You can use (?! Ignore)

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

Exclude the title from the analysis if it was enclosed with a '-' at each end.

0
source

Source: https://habr.com/ru/post/1784984/


All Articles