the data is captured from a textarea to begin with.
Line1
Line2
Line3
etc
It is sent through this function before being stored in the DB (i'm open to better solutions, but if PDO is one of them, I don't understand it and have yet to get it to work)
function test_input($data) {
global $conn;
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$data = mysqli_real_escape_string($conn, $data);
return $data;
}
Which is my way of preventing injections (not my way but a way I found that has worked great until now its giving me this problem with line breaks and textareas)
I try to extract the data from the DB and display it in a textarea, and it shows \r\n instead of doing a line break. It is stored in the DB with the line breaks (i do not see the \r\n but instead i see the data on a new line)
I've tried nl2br, i've tried html_entity_decode, i've tried str_replace \r\n to br (and then it just shows br literal instead of \r\n).
from the research I've found on this site, its the stuff i'm doing to it before i store it in the DB that is causing this but none of the solutions have worked for me.
help.
Replace the \r\n in the text with
before putting it into to the textarea and showing it to the user.
It worked for me.
Try This May be help full
<?php
function nl2br2($string) {
$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string);
return $string;
}
?>
Html versus php and windows (carriage return and newline). Le
rning what happens
when you have a field of values in a buffer. A buffer can be coming from or going to an input/output device
What is inside the buffer may need to be substituted for a string appropriate to the device.
Pulling data from a DB line by line or from a SQL api query will require a regular expression and substitution operation repeating until all
expressions are changed. Bullet proofing the input before it goes into the DB field is always a good idea.
I encountered a print problem that was caused by extra data (escape sequences) that caused the printers
to stop and wait for a reset sequence. No one understood why print jobs failed to print for something like
12 months. I wrote a filter and added it to the printers interface. PRoblem solved
Related
I'm having trouble getting data out of the database and echoing out in a HTML page textarea.
This is the code used to get the data into the database:
$_SESSION['content'] = mysqli_real_escape_string($link, strip_tags($_POST['content'],'<a>'));
This simply strips HTML tags except for links and stores in the database. If you look in the database, the line breaks are invisible but there, so i assume they are \n and \r.
If i were to type into a textarea:
This should be a
New line
The database stores this as:
This should be a<br>
New line
When echoed out into a textarea, this is what's displayed:
This should be a \r\n\r\nNew line
I'm sure i'm missing something very simple, any help greatly appreciated.
UPDATE:
If i remove mysqli_real_escape_string, the line breaks are preserved and work perfectly, do I have to sacrifice security for this?
SOLVED:
mysqli_real_escape_string causing the problem, do not echo out a variable which has had this applied. Only use mysqli_real_escape_string when inserting, deleting, etc from a database, not before, definitely not after ;)
Thanks everyone!
Use the correct HTML/CSS.
;-)
The line breaks all work in an HTML pre tag, or in a tag with the CSS white-space property set to:
white-space: pre;
Resources:
http://www.w3schools.com/cssref/pr_text_white-space.asp
http://www.quirksmode.org/css/whitespace.html
Firstly, you shouldn't be using nl2br as this will change your \n's to <br>'s.
You will most likely need to strip the slashes echo stripslashes($_SESSION['content']).
Edit following further comments:
If the data is stored as <br>'s in the database, you can just do str_replace('<br>',"\n",$string); which will convert the <br>'s into \n's
This worked for me:
function br2nl( $input ) {
return preg_replace( '/\<br.*\>/Ui', '', $input );
}
For very long lines, you also need the text to wrap instead of potentially break out of its parent container. To cover this case, but also preserve new lines, use following css:
white-space: pre-wrap;
resource: https://css-tricks.com/almanac/properties/w/whitespace/
The nl2br function is working with that content as a string:
http://codepad.org/M2Jw9KQJ
This leads me to believe that you are not echoing out the content you claim you are.
Are you sure the content in the DB is as you say it is?
Perhaps you need to go the other way round:
function br2nl( $data ) {return preg_replace( "!<br.*>!iU", "\n", $data );}
I have a form which sends data to a text file, but I'm trying to keep all data sent in the first line of that text file, and replacing would-be breaks with the br tags inside the text file itself. Sorry if there's a really easy solution, but I've been searching and testing for over an hour now >_< (php newbie)
Edit: Yeah here's the general gist of what I currently have. I'm using variables for it.
I have a form with one of the inputs named content that sends data to a submit.php.
In submit.php...
$content = $_POST['content'];
and that sends the following to a text file
$data = "$content";
$fh = fopen("file.txt", "a");
fwrite($fh, $data);
Look at the php function nl2br() (php.net). It does exactly what you need, by going through the string you give it and replacing new lines (\ns and \rs) with <br/> tags.
Apparently nl2br doesn't remove the actual breaks, and only adds the br tags, so try this function:
function oneLiner ($str)
{
$str = nl2br($str);
$str = str_replace(array("\n","\r"), '', $str);
return $str;
}
I'm using PHP to create some basic HTML. The tags are always the same, but the actual links/titles correspond to PHP variables:
$string = '<p style="..."><strong><i>'.$title[$i].'</i></strong>
<br>';
echo $string;
fwrite($outfile, $string);
The resultant html, both as echoed (when I view the page source) and in the simple txt file I'm writing to, reads as follows:
<p style="..."><a href="http://www.example.com
"><strong><i>Example Title
</i></strong></a></p>
<br>
While this works, it's not exactly what I want. It looks like PHP is adding a line break every time I interrupt the string to insert a variable. Is there a way to prevent this behavior?
Whilst it won't affect your HTML page at all with the line breaks (unless you are using pre or text-wrap: pre), you should be able to call trim() on those variables to remove newlines.
To find out if your variable has a newline at front or back, try this regex
var_dump(preg_match('/^\n|\n$/', $variable));
(I think you have to use single quotes so PHP doesn't turn your \n into a literal newline in the string).
My guess is your variables are to blame. You might try cleaning them up with trim: http://us2.php.net/trim.
The line breaks show up because of multi-byte encoding, I believe. Try:
$newstring = mb_substr($string_w_line_break,[start],[length],'UTF-8');
That worked for me when strange line breaks showed up after parsing html.
I'm pulling content from a DB that has been sanitized using mysql_real_escape_string.
Accordingly the new line characters now appear as "\n".
The issue is that this content is displayed to users inside a < pre > tag so I cannot replace \n with < br/> for instance.
I suppose I could replace \n with the actual utf8 character code before inserting the result inside the < pre>.
Can somoneone assist here? Not using mysql_real_escape_string isn't really an option due to security policy.
Thanks.
echo '<pre>'.str_replace('\n', "\n", $string).'</pre>';
why not ...
echo str_replace('\\n', PHP_EOL, "few lines\nof text here,\nblah, blah! etc!");
str_replace("\\n","\n",$data);
I'm pulling content from a DB that has been sanitized using mysql_real_escape_string. Accordingly the new line characters now appear as "\n"
If you've not done anything to the raw data pulled back from the database then the ptoblem is that it has been 'sanitized' twice when inserted.
C.
This might help a bit:
echo('test\ntest');
shows as
test test
but
echo("test\ntest");
shows as
test
test
"replace newline" seems to be a question asked here and there like hundred times already. But however, i haven't found any working solution for myself yet.
I have a textarea that i use to save data into DB. Then using AJAX I want to get data from the DB in the backend that is in TEXT field and to pass it to frontend using JSON. But pasing JSON returns an error, as new lines from DB are not valid JSON syntax, I guess i should use \n instead...
But how do i replace newlinew from DB with \n?
I've tried this
$t = str_replace('<br />', '\n', nl2br($t));
and this
$t = preg_replace("/\r\n|\n\r|\r|\n/", "\n", $t);
and using CHAR(13) and CHAR(10), and still I get an error
the new line in textarea is equivalent to, i guess
$t = 'text with a
newline';
it gives the same error. And in notepad i clearly see that it is crlf
You need to escape all the characters that have a special meaning in JSON, not only line feeds. And you also need to convert to UTF-8.
There's no need to reinvent the wheel, json_encode() can do everything for you.
Prfff... >_< silly me
I've lost another slash before replacing with \n
$t = preg_replace("/\r\n|\n\r|\r|\n/", "\\n", $t);