Jump to content

Remove \r\n from post


asanti

Recommended Posts

I'm currently have this code (part of code):

if (strlen($trimmed['title']) > 3 && strlen($trimmed['title']) < 200) {
        $title = mysqli_real_escape_string($dbc, $trimmed['title']);
    } else {
        echo '<div class="alert alert-danger" id="alerta3">
                <button type="button" class="close" data-dismiss="alert">×</button>
                <center><p>Error</p></center>
                 </div>';
        $errors=true;
    }

    
    if (strlen($trimmed['description']) > 3 && strlen($trimmed['description']) < 4000) {
        $description = mysqli_real_escape_string($dbc, $trimmed['description']);
    } else {
        echo '<div class="alert alert-danger" id="alerta4">
                <button type="button" class="close" data-dismiss="alert">×</button>
                <center><p>Error</p></center>
                 </div>';
        $errors=true;
    }

What should i modify to avoid texts with /n/r?

Thanks

Link to comment
https://forums.phpfreaks.com/topic/292271-remove-rn-from-post/
Share on other sites

So you want to remove newlines characters such as \r\n? Then use str_replace

 

nl2br converts newline characers to a html line break tag <br />. This function should be used when you are displaying text to the user. I do not recommend you using it when saving data to the database.

Link to comment
https://forums.phpfreaks.com/topic/292271-remove-rn-from-post/#findComment-1495705
Share on other sites

the thing is that some posted forms to db have \r\n and others don't. This is why i want to remove them in the form process.

 

As Ch0cu3r suggested, you could use str_replace() to remove them.

 

You could also consider saving the input as is with the newline characters. Then just strip the characters out whenever you display the database content. I guess it all depends on what you're trying to do.

Link to comment
https://forums.phpfreaks.com/topic/292271-remove-rn-from-post/#findComment-1495709
Share on other sites

Ch0cu3r, while I agree that nl2br() is for display, I've had issues in the past with str_replace() not actually replacing line breaks. I don't know if the input was from a Windows machine using just '\n' and I was looking for '\r\n' or if I was searching for '\n' and the input was from a Mac or *nix system that uses '\r\n', or if I was searching for '\n\r' (it was a while ago) but in that situation converting to <br /> and then replacing that with the empty string was the way around it. It did at least give me a consistent string to search for in the string as a whole...

 

Also, asanti - cyberRobot's correct. The data shouldn't be massaged too much beyond validating and sanitizing before storing in the database. You can manipulate the raw data before you output to a variety of media.

Link to comment
https://forums.phpfreaks.com/topic/292271-remove-rn-from-post/#findComment-1495710
Share on other sites

 

Last time I checked nl2br does not remove the newline characters! It only adds <br /> after them. 

 

I have no issues with the following to remove newlines from a string

$string = str_replace(["\r\n", "\r", "\n"], '', $string); 

Wow - just re-read the php manual page and you're right. nl2br() doesn't actually remove the newline characters. I must be remembering incorrectly, or only thought it worked at the time. Hunh...

Link to comment
https://forums.phpfreaks.com/topic/292271-remove-rn-from-post/#findComment-1495714
Share on other sites

 

Last time I checked nl2br does not remove the newline characters! It only adds <br /> after them. 

 

I have no issues with the following to remove newlines from a string

$string = str_replace(["\r\n", "\r", "\n"], '', $string); 

 

Is the "\r\n" needed in that context? Wouldn't the individual "\r" and "\n" cover that?

Link to comment
https://forums.phpfreaks.com/topic/292271-remove-rn-from-post/#findComment-1495716
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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