Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-24-2013, 03:45 PM   PM User | #1
knightmetal
New Coder

 
Join Date: Sep 2012
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
knightmetal is an unknown quantity at this point
Converting plain text into html paragraphs

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 3
OUT;

Output:
$text = <<< OUT
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>
OUT;

Can anyone give me a helping hand? Thanks a lot.
knightmetal is offline   Reply With Quote
Old 05-24-2013, 04:02 PM   PM User | #2
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 597
Thanks: 15
Thanked 67 Times in 67 Posts
Arcticwarrio is on a distinguished road
PHP Code:
<?php
//Input:

$text "<<< OUT
Paragraph 1

Paragraph 2

Paragraph 3
OUT;"
;

function 
P_Convertor($input){
    
$output '';
    
$lines explode("\n\r",$input);
    foreach (
$lines as $k => $v){
        
$output .= "<p>".str_replace("\n\r"""$v)."</p>".PHP_EOL;
    }
    return 
$output;
}

$test P_Convertor($text);
echo 
$test;
?>
__________________
There are 10 types of people on CodingForums,
Those who understand Binary and those who dont.
Arcticwarrio is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:51 AM.


Advertisement
Log in to turn off these ads.