|
Posted by Michael Fesser on 12/28/07 18:19
..oO(kenoli)
>I would like to allow an admin user to enter line returns or html tags
>in a text area, store them in a mysql table and then retrieve them and
>display them in a web page as formatted text, e.g.:
>
>\n\n becomes <p>
>\n becomes <br/>
><ul> remains <ul>
>
>etc.
>
>I've played around with str_replace(), mysql_real_escape_string(),
>nl2br() and can't get it to all work together.
This doesn't help much. What have you tried so far? What does not work
as expected?
>There is obviously both the issue of getting the data into the
>database and then getting it out again and translating (or preserving)
>the data into html.
Just insert the data as it is into the DB (of course with proper
escaping, either with mysql_real_escape_string() or with a prepared
statement). Then on output you could use a function like this:
function nl2html($text) {
$pattern = array('#\r\n?#', '#\n\n+#', '#\n#');
$replace = array("\n", '</p><p>', '<br>');
return '<p>'.preg_replace($pattern, $replace, $text).'</p>';
}
Notice that this function will return HTML, not XHTML. It also only
works for normal text. It will fail if the text itself contains HTML
like a list for example - the result will be invalid code.
Micha
Navigation:
[Reply to this message]
|