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);
Related
I have a string I want to process which looks like this:
HHHHHHHHHHHHHHGGGHHHHHHHHHHHHHHH GGGGGG TTTTT SHHHHHH HHHHHHHHHHHHHHHH
Note how there are multiple spaces between characters. The number of spaces is important, and thus I don't want them to be discarded. When I read this line from a file using php, it prints the following:
HHHHHHHHHHHHHHGGGHHHHHHHHHHHHHHH GGGGGG TTTTT SHHHHHH HHHHHHHHHHHHHHHH
Maintaining only a single space where there were several before. How can I stop PHP from doing this?
Ultimately I want to substring the entire thing into an array of individual characters but I need the array position to be preserved.
Thank you!
EDIT: Example of what I'm talking about:
Replace it with the html entity for a non breaking space to view it rendered in browser's html
<?php
$str = ' HHHHHHHHHHHHHHGGGHHHHHHHHHHHHHHH GGGGGG TTTTT SHHHHHH HHHHHHHHHHHHHHHH';
$str = str_replace(' ',' ',$str);
echo $str;
If you're viewing it with a browser you could use HTML to keep the formatting intact. For instance:
echo '<pre> HHHHHHHHHHHHHHGGGHHHHHHHHHHHHHHH GGGGGG TTTTT SHHHHHH HHHHHHHHHHHHHHHH</pre>';
See: http://www.w3schools.com/tags/tag_pre.asp
I think <code> might also work.
I'm testing a preg_replace function, and I return from an ajax function the processed data (after I process the data through preg_replace, I put it through htmlentities() ):
My test string is:
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
I'm trying to make sure all those characters aren't replaced. My replace function is:
$string = preg_replace('/[^a-zA-Z0-9\s+\n\r,.\/~!##\$%\^&*()\+={}\[\]|:;<>?\'"-]/', '', $string);
I return both the data from "echo" and after going through htmlentities() to see the difference.
when I return the data using alert(data), I get:
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
respectively. However, when I put either of those into $("#div").html(data), I get:
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
so the multiple spaces are lost. Why does the .html() function reduce the spaces? And how can I fix this? Thanks
remove "\s+" from your regular expression and try again
"I'm trying to make sure all those characters aren't replaced." you mean it?
so i make a test like below:
$string = "~!##$%^&*()-+={}|[]:;< >?";
// $string = preg_replace('/[^a-zA-Z0-9]/', '', $string);
echo "'", $string, "'";
output is
'~!##$%^&*()-+={}|[]:;< >?'
if you want keep whole white space between "<" and ">" in $string, i can say that no way, if you wanna output the same white spaces as you input: there are two way can make this:
1> use <pre> tage
2> use replace the white space
does you want these? if you want to keep all? why use regular?
When I have URL like this
site.com/index.php?name=JON%20%20%20%20%20%20%20SMIT
and I print $_GET['name'] variable in index.php file, result is Jon Smit.
That is, queue spaces between JON and SMIT, are replaced on one space. why this happened?
HTML ignores extraneous whitespace, so no matter how many spaces you put in the name, when it is output in a browser, the browser will only show one space. You need to replace your spaces with (non-breaking space) to ensure the browser displays them all.
This should preserve the spaces:
print rawurldecode($_GET['name']);
This will print the extra spaces, however your browser may ignore the extra spaces.
Documentation
How the special chars (like the space) is encodet in the URL is defined, look also Wiki. You can URL encode via urlencode and the inverse of that is urldecode
About the ONE Space only: Look into the source of the Page (i.e. the HTML-Code), in Firefox e.g. via Ctrl+U, there will be many more spaces. BUT: The browser only displays one of them. If you want to preserve them in the output, you could use something like
echo '<pre>' . $_GET['name'] . '</pre>';
OR
echo str_replace(' ', ' ', $_GET['name']);
I've got the following issue with PHP and PostgreSQL.
In a table I added the following value, mark the spaces.
Things: 10 POLI
When I read this out with PHP it will become
Things 10 POLI
My simpified code (for an ideal world without errors) is:
$query = "SELECT stuff, thing, planets FROM 42 WHERE answer = '-'";
$result = pg_query($connection, $query);
$resultTable = pg_fetch_all($result);
Then with
echo "Things: $result[stuff]";
My question is, which step eliminates all the white spaces? And how to get these spaces back? I know that most people want to remove them, I want to keep them.
that is not a PHP issue, but a HTML issue, becauyse if you output with echo, you do in fact generate HTML code.
The HTML specification defines, that multiple consecutive spaces get rendered as only one space.
If you want to avoid this, wrap a <pre> tag around the string:
echo "<pre>Things: $result[stuff]</pre>";
That's because browser does not recognize more than one space, you can use this code to convert consective spaces to (space understood by browser)
$str = str_replace(' ', ' ', $origText);
Or alternatively wrap your text in <pre> tag if that suites your requirements as suggested in comments below.
In my database I have the following text:
for x in values:
print x
I want to print this code on my HTML page. It is printed by PHP to the HTML file as it is. But when HTML is displayed by a browser I, of course, do not see text in this form. I see the following:
for x in values: print x
I partially solved the problem by nl2br, I also use str_replace(' ',' ',$str). As a result I got:
for x in values:
print x
But I still need to shift print x to the right. I thought that I can solve the problem by str_replace('\t',' ',$str). But I found out that str_replace does not recognize the space before the print as '\t'. This space is also not recognized as just a space. In other words, I do not get any before the print.
Why? And how can the problem be solved?
Quote the text in double quotes, like this
str_replace("\t", ' ', $str);
PHP will interpret special characters in double quoted strings, while in single quoted strings, it will just leave the string, with the only exception of \'.
Old and deprecated answer:
Copy the tab character (" ") from notepad, your databasestring or this post, and add this code:
str_replace(' ',' ',$str);
(this is not four spaces, it is the tab character you copied from notepad)
You need to place \t in double quotes for it to be interpreted as a tab character. Single quoted strings aren't interpreted.
always use double quotes when using \t \n etc
It can be tricky because tabs don't actually have a fixed size and you'd have to calculate tab stops. It can be simpler if you print blank space as-is and instruct the browser to display it. You can use <pre> tags:
<pre>for x in values:
print x</pre>
... or set the white-space CSS property:
div.code{
white-space: pre-wrap
}
(As noted by others, '\t' is different from "\t" in PHP.)