krow Posted May 24, 2013 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. Link to comment Share on other sites More sharing options...
Labtec Posted May 24, 2013 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 Link to comment Share on other sites More sharing options...
jeffman Posted May 24, 2013 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. Link to comment Share on other sites More sharing options...
MarkT Posted May 29, 2013 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now