Jump to content

Recommended Posts

Posted
# cat lineendings.txt
text\\r\\n
this doesn't look right\\r\\n
# sed -i 's/\\\\r\\\\n/\\r\\n/g' lineendings.txt
# cat lineendings.txt
text\r\n
this doesn't look right\r\n
#
The -i flag means "in place" so it overwrites the original file. The comical number of slashes (and backslahes) is because you need to escape each \ in regex.
Posted

# is a linux root command prompt (not that you need root privileges to do this, I just happen to be using root right now), and those are commands on the prompt. cat is a command that prints the contents of files to the console. sed is a nifty little command that uses regex to change ouput, in this case a file.

Posted

When I get user input and add into mysql table with trim() function. It shows like

Hi\r\nI am ridoy
on PhpMyAdmin

When I print this out with nl2br function it shows same like above.

Posted

Well, sanitizing your mysql data is a really necessary thing to get in the practice of doing. Figure out what the minimum number of characters that you want people to be able to use, and then do something like this:

$input = preg_replace("/[^a-zA-Z ]+/", "", $_POST["input"]);
What that does is deletes anything that is not lowercase letters, uppercase letters, or spaces.

 

Relevant xkcd: https://xkcd.com/327/

Posted

I just need to fix line break issue. Showing \r\n even while using inside the function of nl2br. All that I want tell you nl2br is not working (maybe). :)

Posted

What that does is deletes anything that is not lowercase letters, uppercase letters, or spaces.

That's exactly what it's supposed to do. Remove everything that isn't lowercase letters, uppercase letters, or spaces.

 

$input = preg_replace("/\\\\r\\\\n/", "\r\n", $_POST["input"]);
!!!WARNING!!!: THE ABOVE CODE DOES NOT SANITIZE PROPERLY TO PREVENT SQL INJECTION!

 

That might do what you want. It hasn't been tested.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...