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
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
Share on other sites

@ BlueSkyIS

 

Maybe I'm a bit dense but I'm not sure I am following you on your first point. Can you elaborate with an example?

 

BTW, thanks for the info on the OS line carriage differences, I thought it was something like that.

 

kaiman

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.