Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

deevo's avatar

Simple Text Area Editor - Like the one for starting a forum thread here

Hey all,

I was wondering if any of you knew (or have any examples) of a very simple text area editor like the one used for starting a forum post here.

All I want to happen is that

tags get inserted. Nothing else. At least for now.

The only examples I can find are way too verbose and more for use in a blog post. I am looking for something that maintains the look and feel of the general text area but creates paragraph tags when the enter key is pressed.

0 likes
9 replies
deevo's avatar

Let me rephrase: I don't really want a wsywig editor as they insert a lot of unnecessary content. What I am really trying to do is capture returns and insert paragraph tags. I really need to up my js skills as I am very uncertain how to do this.

sitesense's avatar

Ah, if that's the case, keep it simple with a textarea.

// create array of textarea input, delimited by linefeed
$lines = explode("\n", $input);

$output = '';

// loop through array and add <p> tag to each line
foreach($lines as $line)
{
    $output.= "<p>". $line. "</p>\n";
}

// do something with output
echo $output;

If you're not really interested in the actual paragraphs themselves, but just linefeeds, simply use:

echo nl2br($textAreaInput);
Snapey's avatar

the problem is, tags don't really belong inside text areas, which is why things like markup exist

have a look at markitup. you can always hide all the buttons if you don't want people bolding and italicising.

You would probably store the markup text in your database and the run it through a parser on output. Alternatively parse it and store the raw version and the parsed version in the database (if performance is a concern). Keep the raw data so that it can be edited later.

sitesense's avatar

@Snapey I wasn't suggesting he inserts tags into the textarea, rather if he wants to display what was inserted into a textarea (after saving to db) in a nicer fashion, it can be as simple as I posted.

If however, the appearance of paragraphs is required whilst editing in the textarea, that's a different story but also easily accomplished.

I haven't time to code it now but with JS or jQuery simply watch the textarea for the 'enter' key (13), then replace the contents, doubling up the "\n" (linefeed).

@tuurbo you do understand that these 'wysiwyg' editors can be configured to allow only the formatting that you choose. For example you could limit the buttons to bold, and underline only?

Anyway, good evening guys.

tuurbo's avatar

@sitesense Yes, i was merely stating that it was possible to do so with contenteditable

Snapey's avatar

@sitesense I was not replying to your post but rather to the op's original question. We posted my reply and your nl2br post at the same time.

My concerns were that you can't actually represent paragraph tags inside a text area. From the question it was not at all obvious that @deevoweb knew this. Also, if manipulating the entered text you have to consider what needs to happen if the user needs to refine their input (maintaining the fidelity of the original input).

deevo's avatar
deevo
OP
Best Answer
Level 5

Well, it turns out after much pain, this is a very very simple solution that is already built into laravel. And it looks like this:

{!! nl2br(e($text)) !!}

That will handle line breaks and show them in the views properly.

1 like

Please or to participate in this conversation.