Then I get a huge space between the heading and the paragraph. It's fine, but if they do the following (which is more likely) heading If the user enters HTML like this: headingsome text I've not tested IE, but nothing ever works on IE!īut I did find another issue. I've tested this on Safari and Chrome and it works fine. $formOUTPUT = str_ireplace(array(''),"\r\n", $previewFORMATTED) So my final code looks like this: $tempCONTENT = mysqli_real_escape_string($con, $_POST) I'm then using a str_ireplace to turn the correct number of back into \r\n so I can echo the result back into the text area to make it easy for them to edit. It just adds the data using mysqli_real_escape_string Using two submit buttons, one to check and another to submit, that doesn't do this. I'm using this to display the input so the user can check it before I add it to the database. It preserves the number of line breaks the user inputs, changing them from \r\n to This is being used to process a text area form field. $previewFORMATTED = str_ireplace(array(''),'', $tempCONTENT) To preserve multiple line breaks I'm doing this: $tempCONTENT = mysqli_real_escape_string($con, $_POST) You can also use strtr() or preg_replace() to achieve the same goal. str_replace() allows you to do this by specifying them in an array. If you want to replace all instances of both these characters, a more reliable way to do it would be to replace them individually, rather than relying on them being next to one-another. It is possible that users may submit the input with only \n without the matching \r, or vice-versa (this may be due to their OS or browser, or a deliberate hack, or a number of other reasons). You may prefer to replace them with a space character rather than nothing. If you replace new lines with nothing, as per your example, the last word of the first line will now run directly into the first word of the second line, and so on. If you want to output to HTML without the formatting, then you may not need to remove them, as the browser will not render line breaks from \n characters. If you want to retain the formatting for output to HTML, then the nl2br() function will help you. If you only want to remove blank lines (and other white space) from the end of the input string, you can use the trim() function instead. Without knowing more about your application, I don't know if this is what you want, but here are some thoughts: If there is more than one new line in the input, they will all be removed. It's worth pointing out that this will remove all new lines from the input, and replace them with nothing. So the direct answer to your question is that you need to use the second of the examples you gave in the question, but with double quotes instead of single quotes: $text = str_replace("\r\n",'', $text) So: '\r\n' will result in a four character string containing a slash, an 'r', another slash and an 'n', whereas "\r\n" will contain two characters, those being the new line and carriage return characters. Note the single-quotes in the first, and double-quotes in the second. In PHP, there is a big difference between '\r\n' and "\r\n". list() : Assign variables as if they were an array.The main problem you have with all the variations you've tried is that both \n and \r are escape characters that are only escaped when you use them in a double-quoted string.First argument is a separator and second is string. explode() : Breaks string into an array.fgets() : Returns a line from an open file.feof() : Checks the end of file is reached or not.Mode parameter specifies the type of access you require to the file. First argument is a file name or url and second argument is mode. Read text file and insert into MySQL databaseįirst let me show you some of the built-in php functions which I am going to use in this process. So in this tutorial I am going to show how to read text file and insert into mysql database. I did that for him.Īt a same time I got an idea why not this small task should share with my PHP beginners. Today I was working on an in house project and suddenly one of my colleagues came to me with a text file and said please import this file into database. Share on Facebook Share on Twitter Share on Linkedin
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |