Preserving enters in user's input by PHP - php

How can you preserve "enters" given by the user in the database and show them then to other users?
I store the question of the user and use the following functions to sanitize the user data, prepare it, and execute the SQL command, respectively.
pg_escape_string
pg_prepare
pg_execute
I use htmlentities with ENT_QUOTES to convert the data HTML.
This procedure removes all enters, apparently in the form \n, in the question.
I would like to have a similar question-system as in SO: to show only double enters to users as line breaks.

After you call htmlentities(), call nl2br(). The web browser ignores plain newline characters so you need to convert them to <br /> elements to make them show up.
nl2br — Inserts HTML line breaks before all newlines in a string
Description
string nl2br ( string $string [, bool $is_xhtml= true ] )
Returns string with <br /> or <br> inserted before all newlines.
For example:
echo nl2br(htmlentities($input));
To only show double newlines and ignore single ones, you could instead use a more sophisticated string replacement function, preg_replace:
echo preg_replace('/\n\s*\n/', "<br />\n<br />\n", htmlentities($input));
Here '/\n\s*\n/' matches a newline, followed by any amount of whitespace, followed by another newline. It replaces any such substring with two <br /> elements. Single newlines are ignored. It's also nice because it'll ignore extraneous spaces and tabs that are invisible, such as if a user typed this:
This is a paragraph.\n
It is pretty short.\n
<space><tab>\n
Here's another paragraph.\n
It too is short.

PHP's nl2br() function should do the trick and allow you to convert \n characters to <br> html tags.
To enable the "two enters for a newline" behavior, you should run a regex to turn every pair of consecutive <br> tags into a single <br> tag (you could also do this with \n characters before running nl2br() on the text).

Related

How does mysql store multiple blank spaces?

I have a PHP script that displays an HTML form that allows a user to enter data in a <textarea> and store it in MySQL.
The user (me) entered multiple lines of text in the <textarea> that included \n, \r, and 4 consecutive space characters (to indent a list I was making). For example:
first line of text:
second line of text:
(A) some task
(B) another task
When the form was submitted, the content was stored correctly in MySQL, including the hidden \r, \n, and the four space characters before (A) and (B).
However, var_dump()-ing the data in PHP shows this:
first line of text.\r\nsecond line of text:\r\n (A) some task\r\n (B)another task
The four space (U+0020) characters I entered do not appear between the \r\n and (A), there is only one space character. I even ran the data through a string to hex converter and it only showed one U+0020 before each (A) and (B) instead of four U+0020s. But, when I re-open the form to edit the data in the <textarea>, the content shows up correctly, just I had entered it originally, with the 4 spaces before the (A) and (B).
My scripts all are behaving correctly and there is no problem. I'm just wondering: How is MySQL and <textarea> able to detect the 4 spaces, but var_dump() only detects one space?
Here is what I have tried to detect the 4 spaces in PHP, with the data stored in a PHP $Variable:
var_dump($Variable)-ing immediately before and after storing the data in MySQL, before/after stripslashes(), and before/after outputting the data to <textarea> and all each var_dump() does not detect detect the 4 spaces.
strpos($Variable) detects the 4 spaces.
print_r($Variable) does not detect the 4 spaces.
echo $Variable does not detect any hidden characters, except for single spaces (not the 4 consecutive spaces)
When you var_dump a variable, it is shown the same HTML.
If you want to show the spaces, you can add white-space:pre-wrap.
The white-space CSS property determines how whitespace inside an element is handled. To make words break within themselves, use overflow-wrap, word-break, or hyphens instead.
With pre-wrap, sequences of whitespace are preserved. Lines are broken at newline characters, at <br>, and as necessary to fill line boxes.

escape sequences are not working in php

if im not wrong \n representation means that a newline as <br> .But when i use <br> or another tags they work properly but escape sequences.
example
echo "write somethings<br>";
echo "about coding";
above example works fine but when i try to use escape sequences none of them are not working
echo "write something\n";
echo "about coding";
it's just an example for newline character and the other escaping characters dont work as \n.What is the real logic on this case?
\n and other similar escape sequences are not part of HTML. You should use HTML escape sequences. These can be found here: http://www.theukwebdesigncompany.com/articles/entity-escape-characters.php
So only your <br> tag works but \n is not
No, this is an example of HTML rules.
Putting \n in a PHP string and then outputting it as HTML will put a new line character in the HTML source code. It's just line pressing return when writing raw HTML.
HTML puts no special meaning on the new line character (at least outside of script elements and elements with various non-default values of the CSS white-space property) and treats it like any other white space character.
<br>, on the other hand, is a line break element (but usually an indication that you should be using a block level element around the content instead).
HTML ignores carriage return and linefeed characters, treating them as whitespace. If you want to use display a string formatted with "\n" you can use nl2br to convert it, e.g.
echo nl2br("this is on\ntwo lines");
If you look at this in the browser it wont work : browser knows only HTML for display (<br>) but not escape like \n or \r

echo backspace inside string

Pretty simple but I'm looking for the easiest way (HEX?) and it's not working...
I want to add to the string backspaces (delete last character)...
Here is my simple code :
<?php
echo '<br>Delete me!!!'."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8";
?>
Small tweak needed here ;)
backspace is a special character that is interpreted by editors and various text input boxes as a request to remove the character before the cursor. The backspace character itself has no magical powers, it cannot make disappear something that was already displayed.
If you need to remove some characters from a string in PHP you can use substring(), str_replace(), preg_replace() or other string handling function.
For example, you can ignore the last 3 characters from a string like this:
echo(substr('blah-blah', 0, -3));

%0D%0A not creating a newline when echo is called

Every time I try to echo a string there is no new line. I how can I make a newline when calling echo in php using the $_GET?
here is my code:
<?php
$text = "Hello world";
$text2 = $_GET['msg'];
echo $text2
?>
and this is what I enter in the url:
http://localhost/hello.php?msg=hello%0Dworld
or this one:
http://localhost/hello.php?msg=hello%0Aworld
and even this one:
http://localhost/hello.php?msg=hello%0D%0Aworld
The echo has to be a newline please don't say I should use a different method than $_GET. It has to be $_GET
While performing your exercises you are creating an HTML page.
HTML is a special markup language, which renders according to set of rules, some of them are:
<> characters has a special meaning of control structures named tags
all newline characters are ignored
to make a newline on the page, one have to use suitable tag - such as <br>, <p> or whatever.
So, to make a newline appear on your page, you have to convert newline characters to tags. Either use nl2br() function to get a <br /> tag or str_replace() if you want any other one
Be aware that echoing any request variables without validating them is a considerable security risk! If you want to publish any application with this code it needs to be redesigned.
As common sense states, the conversion from urlencoded to the corresponding character is automatically done by php, but HTML does not render such characters, so you either need to convert them into linebreaks or enclose the message in <pre> tags.

What is the correct regex (for PHP preg_replace) to remove empty paragraph ( <p> ) tags?

I'm working in Wordpress and need to be able to remove images and empty paragraphs. So far, I've found out how to remove images without a problem. But, I then need to remove empty paragraph tags. I'm using PHP preg_replace to handle the regex functions.
So, as an example, I have the string:
<p style="text-align:center;"><img src="http://www.blah.com/image.jpg" alt="Blah Image" /></p><p>Some text</p>
I run this regex on it:
/<img.*?(>)/
And I end up with this string:
<p style="text-align:center;"></p><p>Some text</p>
I then need to be able to remove the empty paragraph. I tried this, but it removes all paragraphs and the contents of the paragraphs:
/<p[^>]*><\/p[^>]*>/
Any help/suggestions is greatly appreciated!
The correct regex is no regex. Use an HTML/DOM Parser instead. They're simple to use. Regex is for regular languages (which HTML is not).
/<p[^>]*><\/p[^>]*>/ (the regex you gave) should work fine. If it's giving you trouble you could try double-escaping the / like this: /<p[^>]*><\\/p[^>]*>/
PHP is funny about quoting and escape characters. For example "\n" is not equal to '\n'. The first is a line break, the second is a literal backslash followed by an 'n'. The PHP manual entry on string literals is probably worth a quick look.

Categories