We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Multiple Lines breaks removing

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.

4
Contributors
9
Replies
4 Hours
Discussion Span
3 Months Ago
Last Updated
10
Views
Question
Answered
garyjohnson
Junior Poster
142 posts since Aug 2012
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 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));
LastMitch
Industrious Poster
4,118 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

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);
deceptikon
Challenge Accepted
Administrator
3,425 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56

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!

garyjohnson
Junior Poster
142 posts since Aug 2012
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0

My bad, I should have used double quoted strings:

$comment = preg_replace("/(\r\n)+/", "\r\n", $comment);
deceptikon
Challenge Accepted
Administrator
3,425 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56

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

garyjohnson
Junior Poster
142 posts since Aug 2012
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 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;

?>
deceptikon
Challenge Accepted
Administrator
3,425 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56

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

garyjohnson
Junior Poster
142 posts since Aug 2012
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 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.

LastMitch
Industrious Poster
4,118 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

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!

garyjohnson
Junior Poster
142 posts since Aug 2012
Reputation Points: 14
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 3 Months Ago by deceptikon and LastMitch

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1247 seconds using 2.74MB