Jump to content

nl2p function


kaiman

Recommended Posts

Hi all,

 

I am working on debugging a function that converts single breaks in a form text area field into HTML break tags and double breaks into paragraph tags before sticking them into a MySQL DB, so that they can be retrieved and properly displayed on another page.

 

The script seems to work fine for the most part except it seems to be trimming the text around the break tags off. My assumption is in the way that I am trimming the string, but I am not sure.

 

Here is my code:

 

<?php
// turns line breaks in forms into HTML <br> <br/> or <p></p> tags
function nl2p($string, $line_breaks = true, $xml = true)
{
    // remove existing HTML formatting to avoid double tags
    $string = str_replace(array('<p>', '</p>', '<br>', '<br/>'), '', $string);
    
    // convert single line breaks into <br> or <br/> tags
    if ($line_breaks == true)
        return '<p>'.preg_replace(array("/([\n]{2,})/i", "/([^>])\n([^<])/i"), array("</p>\n<p>", '<br'.($xml == true ? '/' : '').'>'), trim($string)).'</p>';
    // convert double line breaks into <p></p> tags    
    else 
        return '<p>'.preg_replace("/([\n]{1,})/i", "</p>\n<p>", trim($string)).'</p>';
}
?>

<?php
// example uses
$text = "Hello.

My name is Kai.";

echo nl2p($text) . "\n";

$text = "Your IP is:
{$_SERVER["REMOTE_ADDR"]}";

echo nl2p($text) . "\n";
?>  

 

Here is what it is returning:

 

<p>Hello.</p>
<p>My name is Kai.</p>
<p>Your IP is<br/>23.123.123.123</p>

 

And here is what it should be returning (notice the missing : and first number of the IP address in the code above):

 

<p>Hello.</p>
<p>My name is Kai.</p>
<p>Your IP is:<br/>123.123.123.123</p>

 

Also, I remember reading somewhere about Mac, Linux, and Windows OS using different break tags could this have something to do with it (I am on Mac OS X), and how would I implement that into this script.

 

Any help or suggestions are appreciated.

 

Thanks,

 

kaiman

 

Link to comment
https://forums.phpfreaks.com/topic/223602-nl2p-function/
Share on other sites

1. i suspect this pattern is removing the preceding and following characters: "/([^>])\n([^<])/i"

 

As you replaced double \n in the first pattern, can't you simply replace single \n in the second pattern?

 

2. Mac is essentially unix. mac/unix/linux line breaks are \n while windows is often \r\n. i guess i would replace all \r with nothing before performing the search for \n

Link to comment
https://forums.phpfreaks.com/topic/223602-nl2p-function/#findComment-1155894
Share on other sites

this might work for you.

 

function nl2p($string, $line_breaks = true, $xml = true) {
    // remove existing HTML formatting to avoid double tags
    $string = str_replace(array('<p>', '</p>', '<br>', '<br/>'), '', $string);
    
    // convert single line breaks into <br> or <br/> tags
    if ($line_breaks == true) {
return '<p>'.preg_replace(array("/\r/","/\n{2,}/", "/\n/"), array('','</p><p>', '<br'.($xml == true ? '/' : '').'>'), $string).'</p>';
    } else {
        return '<p>'.preg_replace("/\n/", "</p>\n<p>", trim($string)).'</p>';
    }
}

Link to comment
https://forums.phpfreaks.com/topic/223602-nl2p-function/#findComment-1155936
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.