Page 1 of 1

nl2p ???

Posted: Wed Aug 31, 2005 7:41 pm
by snx
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.

Posted: Wed Aug 31, 2005 7:44 pm
by feyd

Code: Select all

function nl2p($string)
{
  return '<p>'.implode('</p><p>',explode("\n",$string)).'</p>';
}
:)

Posted: Wed Aug 31, 2005 9:37 pm
by Ambush Commander
Be warned: P renders text with spaces in between, but \n is only one space, so there might be some discrepancy. Although Feyd's function should be fine for most uses.

wow!

Posted: Wed Aug 31, 2005 10:39 pm
by snx
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.

okay check this one

Posted: Wed Aug 31, 2005 10:47 pm
by snx
hi feyd:

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>';
}

Posted: Wed Aug 31, 2005 10:50 pm
by Ambush Commander
Better yet, use this function to sniff out line endings of a file:

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];
    }
And then some...

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;
    }

Posted: Wed Aug 31, 2005 10:57 pm
by s.dot
Having an </p> is optional. So it would be easiest just to convert your \n to <p>

Posted: Thu Sep 01, 2005 6:02 pm
by Ambush Commander
In XHTML, however, </p> is not optional. Generally, I consider it a good practice to keep adding </p>s.

no </p> is not an option

Posted: Thu Sep 01, 2005 9:33 pm
by snx
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...

Posted: Thu Sep 01, 2005 9:36 pm
by Ambush Commander
<br /><br /> Would be the closest. However, when you say:

Code: Select all

<p>Blah blah blah.</p><p>More blah blah blah.</p>
You're designating the blocks as a paragraphs.

When you do this:

Code: Select all

Blah blah blah.<br /><br />More blah blah blah.
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?