Use BB codes (or like here on SO), otherwise the chances are very thin. Function example ...
function parse($string){ $pattern = array( "/\[url\](.*?)\[\/url\]/", "/\[img\](.*?)\[\/img\]/", "/\[img\=(.*?)\](.*?)\[\/img\]/", "/\[url\=(.*?)\](.*?)\[\/url\]/", "/\[red\](.*?)\[\/red\]/", "/\[b\](.*?)\[\/b\]/", "/\[h(.*?)\](.*?)\[\/h(.*?)\]/", "/\[p\](.*?)\[\/p\]/", "/\[php\](.*?)\[\/php\]/is" ); $replacement = array( '<a href="\\1">\\1</a>', '<img alt="" src="\\1"/>', '<img alt="" class="\\1" src="\\2"/>', '<a rel="nofollow" target="_blank" href="\\1">\\2</a>', '<span style="color:#ff0000;">\\1</span>', '<span style="font-weight:bold;">\\1</span>', '<h\\1>\\2</h\\3>', '<p>\\1</p>', '<pre><code class="php">\\1</code></pre>' ); $string = preg_replace($pattern, $replacement, $string); $string = nl2br($string); return $string; }
...
echo parse("[h2]Lorem Ipsum[/h2][p]Dolor sit amet[/p]");
Result...
<h2>Lorem Ipsum</h2><p>Dolor sit amet</p>

Or just use HTML Purifier :)
Dejan marjanovic
source share