|
Posted by kenoli on 12/31/07 21:09
Micha -- Thanks. Your code helped and I have worked something out that
works. I thought I had seen a script someone had designed to do this
and that I might find someone on the list that knew about it.
Happy New Year,
--Kenoli
On Dec 28, 10:19 am, Michael Fesser <neti...@gmx.de> wrote:
> .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]
|