Jump to content

Converting plain text into html paragraphs


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 post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...