nl2p ???
Posted: Wed Aug 31, 2005 7:41 pm
i have been using nl2br to convert multiline form content to text with <br /> 's but i'm looking for a slick way to make it those lines wrapped in a <p>...</p>
thanks
j.
thanks
j.
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
function nl2p($string)
{
return '<p>'.implode('</p><p>',explode("\n",$string)).'</p>';
}Code: Select all
$pieces = explode ("\n\r", $string);
foreach ($pieces as $value)
{
print '<p>' . implode ('<br />',explode("\n",$value)) . '</p>';
}Code: Select all
function detectNewlines($msg) {
$newline_windows = substr_count($msg, "\r\n");
$newline_unix = substr_count($msg, "\n"); - $newline_windows;
$newline_mac = substr_count($msg, "\r"); - $newline_windows;
//gives us our preference, allows us to figure out which one
$array[$newline_mac] = "\r";
$array[$newline_windows] = "\r\n";
$array[$newline_unix] = "\n";
$count = max($newline_windows, $newline_unix, $newline_mac);
return $array[$count];
}Code: Select all
function normNewlines($msg, $newline) {
if ($newline == "\n") {
$msg = str_replace("\r\n","\n",$msg);
$msg = str_replace("\r","\n",$msg);
} elseif ($newline == "\r\n") {
//This routines a bit more complicated, addslashes prevents
//multiple instances of literal \n and then you have that represent
//the whole \r\n before switching it all over. Could
//preg_replace be faster and more intuitive? You never know...
$msg = addslashes($msg);
$msg = str_replace("\r\n","\\n",$msg);
$msg = str_replace("\r","\\n",$msg);
$msg = str_replace("\n","\\n",$msg);
$msg = str_replace("\\n","\r\n",$msg);
$msg = stripslashes($msg);
} elseif ($newline == "\r") {
$msg = str_replace("\r\n","\r",$msg);
$msg = str_replace("\n","\r",$msg);
}
return $msg;
}Code: Select all
<p>Blah blah blah.</p><p>More blah blah blah.</p>Code: Select all
Blah blah blah.<br /><br />More blah blah blah.