asanti Posted November 4, 2014 Share Posted November 4, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/292271-remove-rn-from-post/ Share on other sites More sharing options...
maxxd Posted November 4, 2014 Share Posted November 4, 2014 You can run nl2br() on $trimmed['description'], then do a str_replace() to replace the '<br />' tags with blank strings. Quote Link to comment https://forums.phpfreaks.com/topic/292271-remove-rn-from-post/#findComment-1495700 Share on other sites More sharing options...
asanti Posted November 4, 2014 Author Share Posted November 4, 2014 so it nl2br() would be something like this? $description = mysqli_real_escape_string($dbc, nl2br($trimmed['description'])); Quote Link to comment https://forums.phpfreaks.com/topic/292271-remove-rn-from-post/#findComment-1495701 Share on other sites More sharing options...
Ch0cu3r Posted November 4, 2014 Share Posted November 4, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292271-remove-rn-from-post/#findComment-1495705 Share on other sites More sharing options...
asanti Posted November 4, 2014 Author Share Posted November 4, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292271-remove-rn-from-post/#findComment-1495708 Share on other sites More sharing options...
cyberRobot Posted November 4, 2014 Share Posted November 4, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292271-remove-rn-from-post/#findComment-1495709 Share on other sites More sharing options...
maxxd Posted November 4, 2014 Share Posted November 4, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292271-remove-rn-from-post/#findComment-1495710 Share on other sites More sharing options...
Ch0cu3r Posted November 4, 2014 Share Posted November 4, 2014 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); 1 Quote Link to comment https://forums.phpfreaks.com/topic/292271-remove-rn-from-post/#findComment-1495712 Share on other sites More sharing options...
maxxd Posted November 4, 2014 Share Posted November 4, 2014 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... Quote Link to comment https://forums.phpfreaks.com/topic/292271-remove-rn-from-post/#findComment-1495714 Share on other sites More sharing options...
Psycho Posted November 4, 2014 Share Posted November 4, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/292271-remove-rn-from-post/#findComment-1495716 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.