Johannes
Johannes

Reputation: 67778

Display textarea output as a list with bullets

I have a form with a textarea into which my clients write several phrases/sentences of which typically each one spans one or two lines, and they press the return key to insert a linebreak / begin the next phrase.

Ideally, when this text is fetched from the database and displayed on the webpage, it should have some kind of list format, i.e. a bullet or a - character at the beginning of each phrase.

So it should appear like a typical list with ul and li. But in a plain textarea field you can only do a new-line, no formating. I use nl2br($mytext) to convert these to new-line events to <br> tags in the output. But is there anything similar to convert the whole text into a list or list items (li)?

I know I could use TinyMCE and let them format it, but I don't want them to format anything, I only want these separated lines displayed as a list.

I thought about adding :before pseudo elements containing bullets to the br tags, but those are the end of the lines, not at the beginning - won't work... Any ideas what I could do?

Upvotes: 0

Views: 3698

Answers (1)

Chris Happy
Chris Happy

Reputation: 7295

Split the string by new lines and implode it with '<ul><li>' . implode('</li><li>', $string) . '</li></ul>'.

Upvotes: 2

Related Questions