Tags, quotes, textarea and mysql
am 27.12.2007 23:52:02 von 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
\n becomes
remains
etc.
I've played around with str_replace(), mysql_real_escape_string(),
nl2br() and can't get it to all work together.
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.
I thought I had stumbled across a custom function somewhere recently
that accomplished what I needed to do but can't track it down.
Can anyone help me?
Thanks,
--Kenoli
Re: Tags, quotes, textarea and mysql
am 28.12.2007 19:19:59 von Michael Fesser
..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
>\n becomes
>
Re: Tags, quotes, textarea and mysql
am 31.12.2007 22:09:05 von kenoli
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=A0am, Michael Fesser 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
> >\n becomes
> >
remains
>
> >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) {
> =A0 $pattern =3D array('#\r\n?#', '#\n\n+#', '#\n#');
> =A0 $replace =3D array("\n", '
', '
');
> =A0 return '
'.preg_replace($pattern, $replace, $text).'
';
>
> }
>
> 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