HTML textarea newline character - php

I have a text area in HTML where the user can enter text, but when the form is submitted, and its passed to a php script which echo's it, there aren't any newlines. Knowing that HTML does that, I tried doing a preg_replace() before echoing it...
echo preg_replace("/\n/", "<br />", $_GET["text"]);
but still everything is on one line.
So my best guess is that HTML textareas use a different newline character... can anybody shed some light on the subject?
EDIT
Ok, so I've figured out the problem: The Javascript is stripping the newlines. view code here
EDIT 2
Ok, so thanks to Jason for solving this problem. I needed to do:
escape(document.getElementById('text'));
Instead of just:
document.getElementById('text');
and the newlines are preserved, problem solved!

echo nl2br($_GET['text'])
Though, your preg_replace worked for me!

usually when testing for newlines in any string, I use /[\n\r]/, just to cover my bases. My guess is that this would match the new lines.

Related

PHP Detect 1 or more unicode spaces only

I have searched in vain to find a fix for this issue. I have an editable field in a web page that contains a user entered space. When I copy the space and enter it into a program called IVI32 which I guess you would call a Unicode text program, I get the following info.
The space character is defined as FFFE2000. I need to detect when this field has one or more of these spaces and nothing else. I have tried the following with preg_match:
'/\s+/u'
'/^[0 :-]+$/ '
'/\A\s*\z/'
Nothing works and I am completely stumped. Any help from some Unicode experts out there will be greatly appreciated.
There was an error in my code which was preventing anything from working properly (the product of no time off!). Here's what works for anyone else who might want to detect if an element contains only whitespace that cannot be eliminated by php trim();
if(!preg_match('/\\s/', $test_string)):-do something-
if(!preg_match('/\s+/u', $test_string)):-do something-
if(!preg_match('/[\pZ\pC]+/u', $test_string)):-do something-
For anyone who is interested the space is pasted immediately after the end of this sentence.
Would this work?
preg_match('/^ +$/', $subject);
Match a single or more spaces? Because \s will also match nonbreaking spaces, tabs, and newlines.
Have a try with:
/\p{WhiteSpace}/

Problems with Textareas, New Lines and nl2br

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 );}

How to make a textarea ouput textarea code without breaking?

I created a form where users can enter html code and it outputs their code in another textarea. The problem is that if the html the user enters has a textarea in the code, the in their code breaks my textarea form. I see other sites display any html correctly so how is this done without breaking the form and allowing the user to copy it so that it still remains as and not some converted code so they can paste it on their webpage?
Ah crap yeah I figured it out, in fact the problem wasn't with the htmlspecialchars code alone I forgot to add a return to one of my functions haha. Thanks guys.
Represent characters that have special meaning in HTML using entities. Since you are using PHP, use htmlspecialchars
There are millions and millions of ways to do this. The easiest is to use htmlspecialchars or htmlentities on the user's input. This will make a visual </textarea> in the textarea box without closing it. This actually turns it into </textarea>. htmlspecialchars transforms less characters than htmlentities and usually makes more sense to use in a situation like this, but do your research.
strip_tags() is also a possibility.
You can also use a regular expression with PCRE, or even str_replace() or other string manipulation functions to strip off the textarea, convert the special characters, etc.
PECL also as a BB code extension you can use if you still want your users to be able to enter some for of tags to style their output.
<textarea><?php echo htmlentities($code); ?></textarea>
You have to transform the html code into symbols, so it is not treated as html.
Use the function htmlentities() on the textarea content before echoing it.

Removing Break Lines

I've asked this question before but I didn't seem to get the right answer. I've got a problem with new lines in text. Javascript and jQuery don't like things like this:
alert('text
text);
When I pull information from a database table that has a break line in it, JS and jQuery can't parse it correctly. I've been told to use n2lbr(), but that doesn't work when someone uses 'shift+enter' or 'enter' when typing text into a message (which is where I get this problem). I still end up with separate lines when using it. It seems to correctly apply the BR tag after the line break, but it still leaves the break there.
Can anyone provide some help here? I get the message data with jQuery and send it off to PHP file to storage, so I'd like to fix the problem there.
This wouldn't be a problem normally, but I want to pull all of a users messages when they first load up their inbox and then display it to them via jQuery when they select a certain message.
You could use a regexp to replace newlines with spaces:
alert('<?php preg_replace("/[\n\r\f]+/m","<br />", $text); ?>');
The m modifier will match across newlines, which in this case I think is important.
edit: sorry, didn't realise you actually wanted <br /> elements, not spaces. updated answer accordingly.
edit2: like #LainIwakura, I made a mistake in my regexp, partly due to the previous edit. my new regexp only replaces CR/NL/LF characters, not any whitespace character (\s). note there are a bunch of unicode linebreak characters that i haven't acknowledged... if you need to deal with these, you might want to read up on the regexp syntax for unicode
Edit: Okay after much tripping over myself I believe you want this:
$str = preg_replace('/\n+/', '<br />', $str);
And with that I'm going to bed...too late to be answering questions.
I usually use json_encode() to format string for use in JavaScript, as it does everything that's necessary for making JS-valid value.

PHP form removing line breaks

I use a php form processor script that works fine. Except when users submit text in a multi-line text field, any line breaks or new lines are stripped out of the resulting string variable that is passed on. This often makes it unreadable by whoever receives the form results.
I'm no php expert but am sure the answer lies in the code that is stripping characters. What I'm unsure of is if I stop it stripping characters will this result in a security risk?
The strip array reads:
array('*', '|', '>', '<', '/', '\\\', '\"', 'Bcc', 'BCC', 'bcc');
What can I change here to retain the line breaks?
Thanks in advance for any help.
If your problem is with the submitted string then this means that the submitted string did not contain any line breaks or newline chars.
In one occassion i looked up the wrap="(hard|physical)" attribute on the text area. Some values of this attribute force the textarea to maintain line-breaks in the user text.
Did you try using nl2br($text) on the submitted text;
I believe you have an issue at the rendering phase. Have you tried:
echo nl2br($text);
Where $text is the text you're talking about.
Nothing there is stripping line breaks.
It's more likely that you're not doing anything on the display side to make the line breaks display. You're outputting HTML, and to HTML a line break is just more whitespace. You'll probably benefit from applying nl2br().

Categories