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