Bluehost.com Web Hosting $6.95

How can I preserve leading white space?

This is a discussion on How can I preserve leading white space? within the alt.comp.lang.php forums, part of the PHP Programming Forums category; This simple example: <?php $str=" now, after all the spaces some text"; echo $str; ?> prints: now, ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-22-2005
Dave
 
Posts: n/a
Default How can I preserve leading white space?

This simple example:

<?php
$str=" now, after all the spaces some text";
echo $str;
?>

prints:

now, after all the spaces some text


However, I want to preserve white space, so it prints the leading spaces.

now, after all the spaces some text

Can anyone suggest how to do it?

My actual program (rather than that simple example) prints stdout from
an executable program after the output is opened with popen, read with
fgets and printed with echo - see below. But I think the problem is the
same as in the above simpler code.

<?php

$cmd=$_POST['input_data']; // Read from a form.

$handle = popen("echo $cmd | /usr/local/bin/math" , "r");
$data=fgets($handle);
echo "$data<br>";
?>





--
Dave K

http://www.southminster-branch-line.org.uk/

Please note my email address changes periodically to avoid spam.
It is always of the form: month-year@domain. Hitting reply will work
for a couple of months only. Later set it manually. The month is
always written in 3 letters (e.g. Jan, not January etc)

Reply With Quote
  #2 (permalink)  
Old 11-22-2005
Tyrone Slothrop
 
Posts: n/a
Default Re: How can I preserve leading white space?

On Tue, 22 Nov 2005 04:52:55 +0000, Dave
<INVALID.See-signature-for-how-to-determine@southminister-branch-line.org.uk>
wrote:

>This simple example:
>
><?php
>$str=" now, after all the spaces some text";
>echo $str;
>?>
>
>prints:
>
>now, after all the spaces some text
>
>
>However, I want to preserve white space, so it prints the leading spaces.
>
> now, after all the spaces some text
>
>Can anyone suggest how to do it?
>
>My actual program (rather than that simple example) prints stdout from
>an executable program after the output is opened with popen, read with
>fgets and printed with echo - see below. But I think the problem is the
>same as in the above simpler code.
>
><?php
>
>$cmd=$_POST['input_data']; // Read from a form.
>
>$handle = popen("echo $cmd | /usr/local/bin/math" , "r");
>$data=fgets($handle);
>echo "$data<br>";
>?>


How do you know the spaces are not still there? In HTML you will see
only one space, no matter how many spaces may be in the source.

Do a str_replace (" ", "&nbsp;", $data);
Reply With Quote
  #3 (permalink)  
Old 11-22-2005
Steve
 
Posts: n/a
Default Re: How can I preserve leading white space?


> However, I want to preserve white space, so it prints the leading spaces.


HTML to the rescue! Wrap your output in tags that preserve the layout:

print "<pre>\n";

$handle = popen("echo $cmd | /usr/local/bin/math" , "r");
$data=fgets($handle);
echo "$data";

print "</pre>\n";

---
Steve

Reply With Quote
  #4 (permalink)  
Old 11-22-2005
Dave
 
Posts: n/a
Default Re: How can I preserve leading white space?

Steve wrote:
>>However, I want to preserve white space, so it prints the leading spaces.

>
>
> HTML to the rescue! Wrap your output in tags that preserve the layout:
>
> print "<pre>\n";
>


Thank you - me being a total idiot. I should have realised that myself.


--
Dave K

http://www.southminster-branch-line.org.uk/

Please note my email address changes periodically to avoid spam.
It is always of the form: month-year@domain. Hitting reply will work
for a couple of months only. Later set it manually. The month is
always written in 3 letters (e.g. Jan, not January etc)

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:22 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0