nl2p ???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
snx
Forum Newbie
Posts: 9
Joined: Wed Aug 31, 2005 6:15 pm

nl2p ???

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

function nl2p($string)
{
  return '<p>'.implode('</p><p>',explode("\n",$string)).'</p>';
}
:)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
snx
Forum Newbie
Posts: 9
Joined: Wed Aug 31, 2005 6:15 pm

wow!

Post 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.
snx
Forum Newbie
Posts: 9
Joined: Wed Aug 31, 2005 6:15 pm

okay check this one

Post 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>';
}
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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;
    }
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

In XHTML, however, </p> is not optional. Generally, I consider it a good practice to keep adding </p>s.
snx
Forum Newbie
Posts: 9
Joined: Wed Aug 31, 2005 6:15 pm

no </p> is not an option

Post 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...
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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?
Post Reply