Title: Formatting text for XHTML using nl2p function Marked Cool (Review this resource) Author: kaklz Posted On: 2004-11-24 Category: Home > PHP Functions
Popularity:
Description: I hope all of you know what is a nl2br() PHP function and how you can use it. As we move towards semantic web, there are many places where you want to have pure XHTML. This function converts plain text that has newline characters to a nice XHTML split into paragraphs using <p> tags.
Be sure to execute the downloaded PHP file, in order to see the source and demostration.
Total Hits: 16228 Total Votes: 2
Total Points: 4 (2 reviews) [ Print ]
Page Navigation: [1]
nl2p() functions
I hope all of you know what is a nl2br() PHP function and how you can use it. As we move towards semantic web, there are many places where you want to have pure XHTML. This function converts plain text that has newline characters to a nice XHTML split into paragraphs using <p> tags.
For example, we have some plain text:
This is the first paragraph
This one is the second paragraph
And finally this one is after some couple of line-breaks.
Below you will see a demostration, how this text is formatted using the nl2p() and nl2p_css() functions.
nl2p() demonstration
<p>This is the first paragraph</p>
<p>This one is the second paragraph</p>
<p>And finally this one is after some couple of line-breaks.</p>
nl2p_css() demonstration
<p class="normal">This is the first paragraph
</p>
<p class="normal">This one is the second paragraph
</p>
<p class="normal">
</p>
<p class="normal">
</p>
<p class="normal">And finally this one is after some couple of line-breaks.
</p>
Source code
<?php
/* basic nl2p() function that you can use to format simple
text into simple paragraphs*/
function nl2p ($text){
// put all text into <p> tags
$text = '<p>' . $text . '</p>';
/* replace all newline characters with paragraph
ending and starting tags */
$text = str_replace("\n",'</p><p>',$text);
// remove any cariage return characters
$text = str_replace("\r",'',$text);
// remove empty paragraph tags
$text = str_replace('<p></p>','',$text);
/* optional replacement, if you need a nice-looking
XHTML source and not all source in one line.*/
$text = str_replace('</p><p>', "</p>\n<p>", $text);
return $text;
}
/* the same PHP class, just added a CSS class functionality
- you can also pass a CSS class that you want your paragraphs to use. */
function nl2p_css($text, $cssClass=''){
// put all text into <p> tags, adding the passed CSS class
$text = '<p class="' . $cssClass . '">' . $text . '</p>';
/* replace all newline characters with paragraph ending
and starting tags */
$text = str_replace("\n",'</p><p class="' . $cssClass . '">',$text);
// remove any cariage return characters
$text = str_replace("\r",'',$text);
// remove empty paragraph tags
$text = str_replace('<p class="' . $cssClass . '"></p>','',$text);
/* optional replacement, if you need a nice-looking XHTML source
and not all source in one line. */
$text = str_replace('</p><p class="' . $cssClass . '">',
"</p>\n<p class=\"" . $cssClass . "\">", $text);
return $text;
}
?>
Author: Ingus Rukis, http://www.wisecms.com, ingus.rukis@gmail.com
Page Navigation: [1]
|