nl2p ???
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
function nl2p($string)
{
return '<p>'.implode('</p><p>',explode("\n",$string)).'</p>';
}- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
wow!
i did basically the same thing using:
$pieces = explode ("\n", $string);
foreach ($pieces as $value)
{
print "<p>$value</p>";
}
it works fine except when there is a single new line. i was playing with your lil function nl2p but instead i used "\n\r" works the other way... . i am going to work on a function to split the p's and the br's..
thanks for all the help!
j.
$pieces = explode ("\n", $string);
foreach ($pieces as $value)
{
print "<p>$value</p>";
}
it works fine except when there is a single new line. i was playing with your lil function nl2p but instead i used "\n\r" works the other way... . i am going to work on a function to split the p's and the br's..
thanks for all the help!
j.
okay check this one
hi feyd:
just curious what ya think about this:
just curious what ya think about this:
Code: Select all
$pieces = explode ("\n\r", $string);
foreach ($pieces as $value)
{
print '<p>' . implode ('<br />',explode("\n",$value)) . '</p>';
}- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Better yet, use this function to sniff out line endings of a file:
And then some...
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;
}Having an </p> is optional. So it would be easiest just to convert your \n to <p>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
no </p> is not an option
totally... you have to complete the tags
yes, it works for you and i, but tags are for the machine hence they need to be resolved....
raises a good issue... because it makes sense to have particular tags that dont close
there should be a <p> tag or an equivalent though no?
technicalities...
every other system has them...
yes, it works for you and i, but tags are for the machine hence they need to be resolved....
raises a good issue... because it makes sense to have particular tags that dont close
there should be a <p> tag or an equivalent though no?
technicalities...
every other system has them...
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
<br /><br /> Would be the closest. However, when you say:
You're designating the blocks as a paragraphs.
When you do this:
You have text that has a few spaces in between. There's a huge difference in context.
This isn't really PHP anymore, is it?
Code: Select all
<p>Blah blah blah.</p><p>More blah blah blah.</p>When you do this:
Code: Select all
Blah blah blah.<br /><br />More blah blah blah.This isn't really PHP anymore, is it?