Tags, quotes, textarea and mysql

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.

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
kenoli [ Do, 27 Dezember 2007 23:52 ] [ ID #1894510 ]

Re: Tags, quotes, textarea and mysql

..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
Michael Fesser [ Fr, 28 Dezember 2007 19:19 ] [ ID #1895149 ]

Re: Tags, quotes, textarea and mysql

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 <neti... [at] 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) {
> =A0 $pattern =3D array('#\r\n?#', '#\n\n+#', '#\n#');
> =A0 $replace =3D array("\n", '</p><p>', '<br>');
> =A0 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
kenoli [ Mo, 31 Dezember 2007 22:09 ] [ ID #1896688 ]
PHP » alt.php » Tags, quotes, textarea and mysql

Vorheriges Thema: different between session_id and session_name
Nächstes Thema: How to convert MYSQL data result to csv file format.