I have tried every solution I have come across and am still not getting any results. I have a comment box and I am trying to remove all repetative new lines or /r/n from the input.

if (isset($_POST['comment'])) {  

    $comment = mysql_real_escape_string($_POST['comment']);
    $comment = filter_var($comment, FILTER_SANITIZE_STRING);

    $comment = nl2br($comment);
    //this is where it would remove the new lines
    $comment = str_replace("\n", '', $comment);
    $comment = str_replace("\r", '', $comment);

    if($comment == ""){$error=1;}

        }
else{ $error = 1;} 

for example someone types in Hello/r/n/r/n World!/r/n it would be saved as Hello/n World/n.

Recommended Answers

All 9 Replies

Member Avatar for LastMitch

@garyjohnson

have tried every solution I have come across and am still not getting any results. I have a comment box and I am trying to remove all repetative new lines or /r/n from the input.

You need to add Trim() function to do that.

Try this (I haven't test it yet):

$comment = trim(preg_replace('/[\n\r]/', ' ', $comment));

Try using a regular expression to replace multiple instances of CRLF and newline with a single instance. You probably also want to consider including break tags:

// Squeeze break tags
$comment = preg_replace('/(<br\s*\/>)+/', '<br />', $comment);

// Squeeze CRLF
$comment = preg_replace('/(\r\n)+/', '\r\n', $comment);

// Squeeze NL
$comment = preg_replace('/(\n)+/', '\n', $comment);

Thanks for the response! I have tried both of your suggestions and it is still not deleting the extra /r/n. This is very aggervating!

My bad, I should have used double quoted strings:

$comment = preg_replace("/(\r\n)+/", "\r\n", $comment);

This is still not working, what could the problem be?

Post an example of $comment itself. As an example, this works:

<?php

$comment = "test\r\n\r\n\r\nfoo";
$comment = preg_replace("/(\r\n)+/", "\r\n", $comment);

echo $comment;

?>

I tried a very simple thing,

<?php 
$comment = "test/r/n/r/n/r/nfoo";
echo $comment;
echo "</br>";
$comment = preg_replace("/(\r\n)+/", "\r\n", $comment);
echo $comment;
?>

The first echo displayed the same thing as the second, shouldnt of it displayed test/r/n/r/n/r/nfoo??

Member Avatar for LastMitch

@garyjohnson

Try this:

if (isset($_POST['comment'])) {
$comment = $_POST['comment'];
$comment = mysql_real_escape_string($comment);
$comment = filter_var($comment, FILTER_SANITIZE_STRING);
$comment = nl2br($comment);
//this is where it would remove the new lines
$comment = preg_replace("/[\n\r]/",'',$comment);
if($comment == ""){$error=1;}
}
else{ $error = 1;} 

I only make 3 minor changes:

Adding this:

$comment = $_POST['comment'];
$comment = mysql_real_escape_string($comment);

and this:

$comment = preg_replace("/[\n\r]/",'',$comment);

I'll haven't test it yet either.

Thanks for all the responses and help! I've been messing around with it for awhile and I got this to work

$comment = str_replace(array("\r","\n",'\r','\n')," ", $comment);

Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.