Jump to content

Converting plain text into html paragraphs


krow
 Share

Recommended Posts

I'm trying to write a function that converts plain text into paragraphs wrapped around <p> tags. Here's an input/output example: Input: $text = <<< OUTParagraph 1 Paragraph 2 Paragraph 3OUT; Output:$text = <<< OUT<p>Paragraph 1</p><p>Paragraph 2</p><p>Paragraph 3</p>OUT; Can anyone give me a hand? Thanks a lot.

Link to comment
Share on other sites

So they will essentially be blocks of text? Where are you retrieving this information from - user input, a file, a database? Kind regards, Lab

Link to comment
Share on other sites

1. Normalize the text by converting all "\r\n" sequences to "\n", and all "\r" characters to "\n", in that sequence. If you don't want empty paragraphs, convert multiple "\n" characters to a single "\n". Do that one last. 2. Using "\n" as a delimiter, split the text into an array. Each array element is a paragraph. 3. Loop through the array and add opening and closing <p></p> tags to each element. 4. Turn the array back into a string.

Link to comment
Share on other sites

I'm trying to write a function that converts plain text into paragraphs wrapped around <p> tags. Here's an input/output example: Input: $text = <<< OUT Paragraph 1 Paragraph 2 Paragraph 3OUT; Output:$text = <<< OUT <p>Paragraph 1</p><p>Paragraph 2</p><p>Paragraph 3</p>OUT; Can anyone give me a hand? Thanks a lot.
if you're using PHP fwrite,use
<?PHP $content = WHATEVER YUO WANT."\r\n"; $file = 'filedirectory.txt'; $fp = fopen($file, 'a'); fwrite($fp, '<p>');fwrite($fp, $content);fwrite($fp, '</p>'); fclose($fp); ?>

I use the above code to track IP addresses to certain pages on my CMS. so it writes <br /> after, ie.fwrite($fp, $ipaddress);fwrite($fp, '<br />'); etc. Hope I Helped!

Edited by MarkT
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...