MySql how to not show blank spaces using PHP? - php

I made a board where users can write and post things, and in my board_insert.php file, I have
$content=preg_replace("/(\r\n || \n\r)/","<br>",$content);
in my mysql database, because of all the spaces, the table is very hard to read.
so I want to replace all the spaces in content column to <br>. I put both \r\n or \n\r because I didnt know which one meant 'space'.
Is there a better way to do this?
maybe not show the content part in mysql at all?
because it is not necessary for me to see content part in mysql when I can see it on the board_read page...
Thank you in advance!

Try this
$content=preg_replace("/\r?\n/","<br>",$content);
That said, you may be better of using another method then editing the data,
This is one way http://docs.php.net/manual/en/function.nl2br.php
using a <pre></pre> tag is another less invasive one. and the best is simple
<div style="white-space: pre"> content </div>

You can use
$content = str_replace(array("\n","\r"), "", nl2br($content));

Normally you would insert the data as is into the database, meaning with the \n and/or \r. When you display you can use nl2br() to display it as HTML.

Related

PHP wont show new line [duplicate]

This question already has answers here:
Echo from MySQL database with spaces and line breaks?
(2 answers)
Closed 9 years ago.
i have a little problem with the text to be readed from my database.
After the user has confirmed their new post, it saves in the database like this ( like i want it to do).
but in the webpage, it will ignore these lines, and just echo out everything on the same line.
Here is a bit my source code:
$objekttekst=str_replace("\\r\\n", "<br>", $obj->innhold);
$objekttittel=$obj->tittel;
?>
<h2><?=$objekttittel?></h2>
<p><?=$objekttekst?></p>
could someone help me out? thanks
Use nl2br() function.
$objekttekst = nl2br($obj->innhold);
The input textarea is pre-formatted, which means that it will show any newlines that the user enters. However, HTML rendered (web browser) does not display any newlines from the input, unless newlines are explicitly inserted with tags such as <BR>.
You have several options here. For sure these three are not your only options, but they are the ones I have personally been using most often.
Form textarea with pre-formatted text
If you want to display the data (objekttekst) in a similar textarea where the input was given, you could do:
<h2><?=$objekttittel?></h2>
<p><textarea><?=$objekttekst?></textarea></p>
This would suit you best in a situation where the user needs a possibility to edit the entry.
Preformatting
If you want to display the text as it is, you can always surround it with <PRE>...</PRE>. That will show any newlines, indentations etc. Note that this will make the output use a fixed-width font such as Courier New.
Convert newlines to <BR> tags
Use function nl2br() as already mentioned in another answer. See: http://php.net/manual/en/function.nl2br.php for more information.
Additional note...
You might want to look into regular expressions, as in many cases you might want to do also some other modifications to your data before showing it in the HTML page. nl2br() will take care of newlines, but for other and more complex modifications you should learn regular expressions.
You can surround your string with <pre> tag instead of replacing \n with <br>
Example:
<?php
$objekttekst=$obj->innhold
$objekttittel=$obj->tittel;
?>
<h2><?=$objekttittel?></h2>
<p><pre><?=$objekttekst?></pre></p>

How should i store newlines in a MySQL database?

I'm going around and around in circles in my mind trying to decide how best I should store newlines in a MySQL database.
A bit of background; the user is asked to complete a textarea. This has to stored in a MySQL database before being read back out and the content included in an HTML email.
Should I store them as:
1) String literal - surely this is dangerous and bad practice
2) As a string with \r\n in - when I read this back out of the database its read as 4 characters so nl2br() fails to correctly replace them.
3) As HTML <br /> - as it has to be html entity encoded to be stored it ends up being stored as <br /> so when it gets to the email <br /> is printed rather than an actual newline. Passing it through html_entity_decode() would decode other characters that need to be encoded.
Any help grately appreciated.
Store it as it is but escape first. This is your option 1. When you need to present this data apply whatever function you need to format it. If you need to show it in HTML use nl2br() and htmlentities() functions. That'll work for mail also.
If you have a text area data like this,
$text="Hello Sir,
I am Robert'); Drop table students; --
.....
Yours most obedient pupil
.....";
Just store it as it is after you escape it, or use a prepared statement.
$text = $mysqli->real_escape_string($text);
use the javascript string replace function str.replace(/\n/g,'\\n')
to transform all newlines to escaped literals in the textarea while forming the url or post string.
Retrieving the same using PHP's json_encode() and displaying it through a textarea works well for me.
It depends on application type (how you want to display your text).
If this is HTML e-mail editor - i think html format will be better.
In most CMS (content management systems) HTML format is used (joomla, wordpress, prestashop etc.), because you can just read this from database and send to browser.
Besides - you probably may need other HTML tags anyway (like <b> for bold or <center>).
I think using \r\n format is better for non-web applications, when data is displayed on windows form controls.
One more thing - you may store them in both ways:
- <br> for HTML view
- \r\n for HTML source (to add some newline's and make more readable html source code)
By more readable html i mean this:
<center>
This is header
</center>
<p>
This is paragraph.
<br>
Second line.
</p>
Instead of this:
<center>This is header</center><p>This is paragraph.<br>Second line.</p>

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: Convert HTML for use in alt/title attribute in a tag and keep the formatting

Essentially I have this
<p>hello</p>
<p>So I wanted just to say hi</p>
<p>I hope its going well</p>
Coming from a db.
If I just strip the tags then in the title I get this
title="helloSo I wanted just to say hiI hope its going well"
What I want (like SO does it).
title="Hello
So I wanted just to say hi
I hope its going well"
I have tried using \n or \r and it just shows up in the title text.
title="Hello\n\rSo I wanted just to say hi\n\rI hope its going well"
Is this possible because of using Markdown and the way SO is saving the text?
I'm using TinyMCE and I have looked into ways of formatting the text, I've even tried using the output buffer to try and arrange the text how I want it.
Edit: Lets make this really clear and simple. It's going into a TITLE element!
If I can't get this to work, I'll just do this via a popup with jQuery.
Any help / advise appreciated :)
In PHP, The string '\r\n' is interpreted just like that and consist of four letters. The string "\r\n" is interpreted as escape sequences and contains two characters, a carriage return and a linefeed. Use double quotes if you want to insert a line break in a string.
If I recall correctly, browsers won't render alt/title tags with multiple lines? It will simply remove the line spacing.
If they do now, then PHP requires you to wrap \r\n\t and other formatting in " double quotations as Sjoerd suggested.

Categories