Jump to content

Php Is There Any Way To Convert From \\r\\n To \r\n


ridoy

Recommended Posts

# 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.
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.   Paste as plain text instead

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