fergusdawson
fergusdawson

Reputation: 1765

how to insert <p> tags where there is a line break

How can I automatically insert <p> tags into a block of text?

I want to turn this:

"Sed quis dignissim sem. Sed porttitor metus non ligula tempus at pellentesque magna ultrices.

Pellentesque fermentum dignissim lacus, eget consectetur ipsum dictum a. Sed fermentum velit vitae magna scelerisque blandit. Suspendisse potenti. Vestibulum id tellus quis velit dictum lacinia."

into this:

"<p>Sed quis dignissim sem. Sed porttitor metus non ligula tempus at pellentesque magna ultrices.

<p>Pellentesque fermentum dignissim lacus, eget consectetur ipsum dictum a. Sed fermentum velit vitae magna scelerisque blandit. Suspendisse potenti. Vestibulum id tellus quis velit dictum lacinia."

Upvotes: 0

Views: 188

Answers (2)

dlgrasse
dlgrasse

Reputation: 308

if you want html tags in your output, you must encode them. so to show a <p> tag (like I just did) you should type in &lt;p&gt;

Upvotes: 0

Jamund Ferguson
Jamund Ferguson

Reputation: 17024

Where is your text coming from?

You'll need to use either Javascript or a server language (PHP?) to do this.

In most languages something like this will do it though:

var string = "lakjdsf lkjafsd jkladfsjlk adfslk jafsl kadfslk jadfsl kdjk l jklaj kladfsj kladfsj kl"
var html = string.replace(/\n/g, "<br>")

PHP has a built-in function that does this called nl2br:

<?php echo nl2br("foo isn't\n bar"); ?>

Upvotes: 2

Related Questions