Textarea Adding Space To Beginning of Text? - php

I'm having a strange problem in that I have php inserting text into a <textarea> and the <textarea> is adding one white space to the top of my text.
I created an example page to display the problem... here it the code behind the page.
<textarea style="width:600px;height:100px;"><?php get_film_info('main description'); ?></textarea>
<br>
<textarea id="mainDescription style="width:600px;height:100px;">Text just typed in</textarea>
<br>
<?php get_film_info('main description'); ?>
You can see that without the <textarea> tag, the text does not include the indent. My database also reflects no indent, as well as the php output outside of the <textarea>...
Any clue what could be going on?
the sample page
Edit: You were all right, of course I didn't bother checking the source code of the output file. Turns out when I was adding the data (via ajax) I was sending my data like var data = '&main_description= ' + mainDescription. Notice the space between the "=" and the "+".
Thank you all for your input, gotta just give the check mark to the guy on the top of the list.

Try this:
trim(get_film_info('main description'));

Your text has space at beginning!
I don't know what function 'get_film_info' returns but it returns with space!

There's definitely a space in the beginning and one at the end, as can be seen in the page source. Perhaps get_film_info() is inadvertently injecting them.

There is a heading space in the return value of get_form_info(). Check the value of 'main description' in your database (or whereever it is being stored). If there isn't a heading space in the value itself, then get_film_inflo() is to blame.

A space does exist. Outside of the textarea, the browser does not interpret them, because a \n means nothing (it is interpreted in the source code only) in just regular text form. However, a \n inside a textarea represents a line break and is being interpreted as such.
To solve the problem you can always trim the value before outputting it.

Related

How to preserve line breaks in textarea?

I went through similar questions on this issue but couldn't find a solution.
I have a simple textarea in which text will be pasted or entered directly.
How do I preserve those line breaks?
Right now line breaks will just be ignored, but when I print_r($_POST) and inspect the output in the Chrome Developer Console it still has the original text breaks.
How can I display those when showing the submitted data?
Right now line breaks will just be ignored
No, it is still there. The key you miss is that what you enter goes as ASCII code LF (0xA) but this makes no effect in HTML unless the text is displayed as preformatted block (<pre> ... </pre>). If not, then line break is represented bytag so what you need to do if you want to display entered text is to convertLFs to(you can usenl2br()function to do that) or use` block.
You can use nl2br() function:
Inserts HTML line breaks before all newlines in a string
echo(nl2br($_POST));
More information at http://php.net/manual/en/function.nl2br.php.
As Marcin noted, use nl2br() when displaying your data as instructed in this answer: https://stackoverflow.com/a/6480671/6206601

MySql how to not show blank spaces using 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.

What's the trick to display a text from MySql/PHP back to its original HTML layout?

It still keeps the original text layout (I mean the spacing, offsets, new line, paragraphs) while the text fragment is stored in MySql ('text' type) field - I can tell when I peer into it in my DB browser (Adminer:)
but it gets lost when I output it from the DB: it becomes a single line string of my text characters. How can one restore it its original layout?
I've tried to reshape the text fragment using the PHP nl2br() function with some success:
it brought back the newline breaks, but the text words positioning is not kept, everything
shifts to the left.
Thanks in advance for a good idea.
If you've got multiple spaces and things like that. e.g. for code. Then trying using the pre tag.
http://htmldog.com/reference/htmltags/pre
http://reference.sitepoint.com/html/pre
The html_entity_decode() function converts HTML entities to characters.
The syntax is:
html_entity_decode(string, [quotestyle], [character-set]);
You can refer example2.

PHP: How to prevent unwanted line breaks

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.

Actual input contents are not preseving on most of the browsers [FF,MSIE7/8 and etc]

I'm working on one application ( using PHP, javascript ). Below is the short description about my problem statement
There are two forms avaliable on my application, i.e. SourceFrm and targetFrm.
I am taking input on first form i.e. SourceFrm and doing processing on targetFrm.
Below is the input which I am taking from SourceFrm :
1) Enter your data (Identification of this input box id is 'inputdata' ):
2) Enter id ( Identification input box id is id ):
As per above input feed by user I am posting this data to targetFrm for further processiong.
On TargetFrm :
I am simply assigning inputdata value to php varible.
The spaces which are in between of words are getting lost ( more than one spaces converting to one space).
e.g.
User has added below data on input box and submitted
inputdata:
This is my test.
Here observed that user has added 5 spaces in between 'my' and 'test' word.
After assigning this input data to php variable. After that I printed this value
Below content I am getting
Output:
This is my test.
More than one spaces is converting to one space. This behaviour I checked on all browsers like FF,MSIE7/8 opera, safari, chrome.
If have used '<pre>' before printing php variable i.e.:
print "<pre>";
print $inputdata;
At time spaces are not getting lost (I am getting exact content).
Here my conflict is how do I presrve exact contents without using '<pre>'.
I have used encoding/decoding (htmlentitiesencode() and decode () )functionality, in my further data processing, so it may create some conflict if i replace spaces with . ( May conflict ll occur if i use instead space ).
Is anyone has any ideas, suggestions please suggest.
-Thanks
When you output your variables to HTML, they are parsed as HTML. Any additional white space is brought down to one space.
A simple fix would to replace all spaces with the html entitity to force browsers to display each space.
I wouldn't store the string with all the &nbps; in the database, but when you show it the would ensure that each space is seen.
EDIT
I mean only replace spaces on render...like:
print str_replace(' ', ' ', $inputdata);
HTML is capable of showing only one space. I'm not really sure why, but if you check your source code of rendered webpage containing your string, you'll see that it contains all the space, the browser just doesn't show it.
The same is for other space characters, as tabs.
The way to deal with it depends on type of your content. You can either replace spaces with or leave it as it is or do something completely different, i.e. strip more than one space down to one space.
It really depends on naturel of your data–the only real situation, when you would need more spaces than one, that comes to my mind is if you're trying to indent things with spaces, what actually isn't that great idea.
Edit: older resource:
http://www.sightspecific.com/~mosh/WWW_FAQ/nbsp.html

Categories