The following code outputs a b:
$var="a b"; // I inserted 3 white spaces, and HTML is rendering only one
echo $var;
The issue is that if I store $var in a table, it will KEEP the white spaces, and while reading, it strips them back. The striped data gets as it is updated in another table, resulting in mismatch of same values in both the tables.
I tried google search and found in a thread that its how HTML behaves. Any hope on how can I fix this?
The normal white-space behavior is to collapse everything to one space. You can modify it by changing the white-space: css rule to white-space: pre; or white-space: pre-wrap;
See https://developer.mozilla.org/en/CSS/white-space for more details
Multiple spaces are trimmed in the html output of most browsers, not in the html code itself. If you want multiple spaces to render you can use str_replace(" ", " ",$var) which will tell the browsers to render each whitespace character. The same goes for multiple linebreaks, using nl2br()
HTML always strips extra whitespace characters (for displaying). If you want to prevent this use . However it sounds like you are using it for alignment. Never do that!
What if the text changes?
Related
Let's say I have a variable $foo:
$foo = " a a blablabla a a";
But when I do var_dump($foo) the following gets outputted:
string(57) " a a blablabla a a"
It's like the length (57 in this case) it's correct and it counts the spaces, but it doesn't display them.
How can I display the full string, including the multiple spaces in between the other characters?
If you use <pre> tag the text appears in the brower as you typed it. Couple of links:
http://www.tizag.com/htmlT/htmlpre.php
http://www.htmlcodetutorial.com/linepar/_PRE.html
You could also replace spaces with non breaking space .
Web browsers ignore a lot white space in the code which is nice. Otherwise we wouldn't be able to intend our source code or use newlines much...
Are you by any chance doing this in a browser. If so you need to do this.
<pre>
<?php echo var_dump($foo); ?>
</pre>
Because I just tried in command line and I get the output with spaces. Browsers don't handle multiple spaces and they trim them down. If you want a browser to handle multiple spaces you have to use the output inside a <pre> tag or use instead of spaces.
something like echo str_replace(' ', ' ',$test);
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'm very new to PHP so I know I am missing something obvious here -
I thought the heredoc function is supposed to retain formatting, line breaks, etc.
But whenever I test it, while it parses, there is no formatting.
I've tried lots of different scripts including copy-and-pasts from sources such as PHP.net and W3schools - so I know there is nothing wrong with the scripts. Can't google up an answer to this one - probably because it's too obvious?? (BTW, testing with MAMP). Thanks.
HEREDOC is not a function, it is a method for specifying string delimiters that allows you to forgo escaping quotes within the heredoc (I like it because good text editors will syntax highlight their contents based on the heredoc name).
HEREDOCs do preserve all whitespace, newlines, etc. I think you are probably looking at the results in a browser. Browsers generally trim all whitespace, newlines, spaces, and tabs included, down to a single space. Try looking at it on the command line, a text email, or a text file.
It will produce a string identical to the one you set.
However, browsers render multiple whitespace condensed to one space character. This is by design.
To preserve your spaces, you can use the pre element (assuming default browser stylesheet) or white-space: pre CSS property.
<div style="white-space: pre">
<?php echo <<<A
some text
preserved
just
how
you want it.
A; ?>
</div>
jsFiddle.
Because you said "testing with MAMP" I assume, that you use your webbrowser to display the content. Thats a slightly wrong approach, because the webbrowser itself strips down any unnecessary whitespaces. Look at the source of the page you display in your browser and you will see the "real" content.
It does, but only for what it outputs.
If it outputs HTML, then that HTML is going to be interpreted by a browser using the normal rules for handling whitespace.
I've got some data that needs to be cleaned up into a fixed length format. I'm using PHP to grab the data out, covert it, and put it back in, but it's not working as planned. There is a certain point in each piece in the middle of the data where there should be spaces to increase the length to the proper amount of characters. The code I'm using to do this is:
while ($row = mysql_fetch_array($databasetable)) {
$key = $row['KEY'];
$strlength = strlen($key);
while ($strlength < 33) {
$array = explode(' TA',$key);
$key = $array[0] . ' TA' . $array[1];
$strlength++;
}
}
It's taking a ' TA' and adding two spaces before it, rinse and repeat until the total length is 33, however when I output the value, it just returns a single space. Funny part is that even though it is displaying a single space, it returns a strlen of 33 even if it's not displaying 33 characters.
Any help in figuring this out would be greatly appreciated.
HTML will have extra spaces filtered out.
To force extra spaces to be shown, use ' ' rather than ' '.
#TVK- is correct, HTML ignores multiple-space whitespace - it'll turn it into one space.
In addition to his solution, you can use the CSS instruction white-space: pre to preserve spaces.
Remember that, when doing an output to a webbrowser, it'll interpret it as HTML ; and, in HTML, several blank spaces are displayed as one.
A possibility would be to use the var_dump() function, especially if coupled with the Xdebug extension, to get a better idea of your data -- or to display it as text, sending a text-related content-type.
BTW : if you want to make sure a string contains a certain amount of characters, you'll probably want to take a look at str_pad()
Easiest options for you I think are
Wrap your output in <pre> tags
replace each space with
If you're rendering HTML, consecutive spaces will be ignored. If you want to force rendering of these, try using
Generally using multiple non breakable spaces one after another is a bad idea and might not bring a desired result unless you're using a monospaced font. If you want to move some piece of text to a certain position on your page, consider using margins
You can tell the browser that the text is preformatted
this text will be displayed as it is formatted
spaces should all appear.
new lines will also be apparent
Have you looked into str_pad? something like :
str_pad ( 'mystring' , 33 , ' TA' , STR_PAD_LEFT );
I thing you can use str_repeat
echo str_repeat(" ", 15);
How can I add space before numbers in PHP, to get output like the following?
9
10
100
I'm using str_pad($k,3,'',STR_PAD_LEFT), but blank space is not working. And I don't want leading zeros, but I want blank spaces.
You may be looking for str_pad().
foreach (array(1,2,3,100,1000) as $number)
echo str_pad($number, 10, " ", STR_PAD_LEFT);
However, if you want to output this in HTML, the multiple spaces will be converted to one. In that case, use as the third parameter.
Also, if you use a font like Times or Arial, this will never produce perfectly exact results, because characters differ in width. For perfect results, you need to use a Monospace font like Courier.
Anyway, check #Mark Baker's answer first. Right aligning the data using CSS's text-align: right is the best solution in most cases.
If you're displaying the result in a web browser, then you should be aware that browsers have this nasty little tendency to convert multiple white spaces to a single space. To check if your code is working, use the browser's "view source" option and count the spaces in the actual source rather than the rendered display.
Then look at alternatives such as or right-aligning your values using CSS.
When printing the numbers:
printf("%5d\n", $number);
Replace the 5 with how many spaces width minimum you want.
Use the function str_pad for this purpose.
This is what you are looking for:
str_replace(" "," ",str_pad($number, 5,' ', STR_PAD_LEFT));
grml, look into my comment. I don't know why here the nbsp :) is gone