1,105,380 Community Members

Multiple Lines breaks removing

Member Avatar
garyjohnson
Junior Poster
168 posts since Aug 2012
Reputation Points: 14 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

LastMitch
Deleted Member
 
0
 

@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));
Member Avatar
deceptikon
Eternally Awesome
4,693 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 688 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

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);
Member Avatar
garyjohnson
Junior Poster
168 posts since Aug 2012
Reputation Points: 14 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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!

Member Avatar
deceptikon
Eternally Awesome
4,693 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 688 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

My bad, I should have used double quoted strings:

$comment = preg_replace("/(\r\n)+/", "\r\n", $comment);
Member Avatar
garyjohnson
Junior Poster
168 posts since Aug 2012
Reputation Points: 14 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
deceptikon
Eternally Awesome
4,693 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 688 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

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;

?>
Member Avatar
garyjohnson
Junior Poster
168 posts since Aug 2012
Reputation Points: 14 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

LastMitch
Deleted Member
 
0
 

@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.

Member Avatar
garyjohnson
Junior Poster
168 posts since Aug 2012
Reputation Points: 14 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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!

Question Answered as of 1 Year Ago by deceptikon and LastMitch
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: