krow 0 Posted May 24, 2013 Report Share Posted May 24, 2013 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. Quote Link to post Share on other sites
Labtec 4 Posted May 24, 2013 Report Share Posted May 24, 2013 So they will essentially be blocks of text? Where are you retrieving this information from - user input, a file, a database? Kind regards, Lab Quote Link to post Share on other sites
jeffman 86 Posted May 24, 2013 Report Share Posted May 24, 2013 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. Quote Link to post Share on other sites
MarkT 8 Posted May 29, 2013 Report Share Posted May 29, 2013 (edited) 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 May 29, 2013 by MarkT Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.