Jump to content

new line /r/n preg replace problem -> <br>


rickyj

Recommended Posts

Hi there, I’m having a real bit of trouble, and I cant narrow it down.

 

I’m trying to get a form to post a message to a mySQL data base, and replace the users input new lines into <br>

 

- But this doesn’t seem to  be working as expected.

- I have tried lots of things, but they all give strange results.

- the Mysql column is a text column

- I have tried using nl2br, but this does nothing to the text.

 

Im currently using:

function nl2br2($string) {
    $string = str_replace('\r\n', '<br>', $string);
    $string = str_replace('\r', '<br>', $string);
    $string = str_replace('\n', '', $string);
    return $string;

 

This works perfect, unless the new line start with an n, if the new line starts with an n the n gets striped:

 

first line

new line

third

 

gets converted to

 

first line<br>ew line<br>third

 

if I use this:

 

function nl2br2($string) {
    $string = str_replace('\r\n', '<br>', $string);
    $string = str_replace('\r', '<br>', $string);

    return $string;

I get:

first line

\nnew line

\nthird

 

I cant figure out whets going on here?

I have also tried using variations, but get exactly the same result:

 

 

function nl2br2($string) {
$string = str_replace('\n\r\n', '<br>', $string);
$string = str_replace('\r\n\n', '<br>', $string);
$string = str_replace('\r\n', '<br>', $string);
    $string = str_replace('\r', '<br>', $string);
    $string = str_replace('\\n', '', $string);
    $string = str_replace('\nn', ' n', $string);
    $string = str_replace('\n', '', $string);
    return $string;

 

gets converted to

 

first line<br>ew line<br>third

 

function nl2br2($string) {
$string = str_replace('\n\r\n', '<br>', $string);
$string = str_replace('\r\n\n', '<br>', $string);
$string = str_replace('\r\n', '<br>', $string);
    $string = str_replace('\r', '<br>', $string);
    $string = str_replace('\\n', '', $string);
    $string = str_replace('\nn', ' n', $string);
    return $string;

I get:

first line

\nnew line

\nthird

 

Any on know what is happening?

 

Link to comment
Share on other sites

When using escape characters use double quotes and not single quotes.

 

However you may aswell use nl2br to do what you are trying to accomplish. However only use nl2br when you get the data out of the database not when you insert the data into the database.

Link to comment
Share on other sites

Don't know if this is any help mate, but a while back a forum member wrote this function which iuse regularly:

 

     function CleanPosts($String, $nlbr = false) {

        if (get_magic_quotes_gpc()) {
     
          $String = stripslashes($String);
     
          } 
         
        if ($nlbr) {
     
          $String = nl2br($String);
     
     }  

 

works great:)

 

Graham

Link to comment
Share on other sites

try this

$string= preg_replace('/\r\n|\r|\n/', '<br>', $string);

 

Thanks for the help,

I tried this but get the following for:

1st line

new line

third line

 

gets converted to :1st line\r\nnew line\r\nthird line\r\n

(looking at the database: looks exactly the same)

i.e., it doesn’t get converted at all.

 

yet,

$string = preg_replace('\r', '<br>', $string);

works but leave /n at the start of each line, and

$string = preg_replace('\r\n', '<br>', $string);

works, but deletes n's off of the start of a new line if its present <-- this I find really odd!

 

Link to comment
Share on other sites

my above code was supposed to say str_replace (The edit function was not working for me?). preg replace doesnt seem to work here for some reason?

 

However, It should be noted, before I use these variables, I get them via this method:

 

stripslashes((get_magic_quotes_gpc()) ? $_POST[$MyString] : addslashes($_POST[$MyString])));

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.