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.
Related
I have stored some html data into database through summernote plugin
in database it look like this
<p><span id="job_summary" class="summary"><ul><li>
<b class="jobtitle"><font size="+1">Analyst/Junior Analyst- Outbound calling process</font></b>
here is how i show it
echo text_cut(strip_tags(html_entity_decode($ro)),300);
now i want to show this data in plain text on my page, i tried using strip_tags but it makes the looks messy, here is how it looks after strip tags
knowledgeMust be reliable in terms of attendance and timingExhibit
it joined the words, so now i want all the html tags to be converted into how can i achieve this
Try This,
$spaceString = str_replace('<', ' <', $ro);
echo strip_tags(html_entity_decode($spaceString));
Here's something you can try. If you replace the tags with a space and then replace multiple spaces with one space, it should give you the desired results.
First, use something like this to replace the tags:
preg_replace('~<.*?>~i', ' ', $string);
Here is what it will give you
Next, you can look for multiple spaces in a row and consolidate them:
preg_replace('~ +~', ' ', $string);
That will give you this:
Analyst/Junior Analyst- Outbound calling process
Here is a demo of it all together
You won't really be able to see it, but there is a line above it with a blank space and a blank space before the string as well. So, depending on what you are wanting the result to look like, you can use a \s+ instead of [SPACE]+
Here is another demo showing how to do it that way
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.
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'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);