EllisLab text mark
Advanced Search
1 of 3
1
   
2 trailing [EDIT:] leading whitespaces in output
Posted: 02 May 2009 10:25 AM
Joined: 2008-11-08
85 posts

Hi all,

I just found out, that all pages generated by CI, have 2 trailing whitespaces at the beginning of the page.
As i have a third-party accessing a page, which expects to have no trailing whitespaces, does somebody know how to prevent that behaviour?

$this->output->set_output($data); 

will have 2 trailing whitespaces, too.

Can anybody point me in the right direction?

Thanks,
Michael

 
Posted: 02 May 2009 10:29 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2009-05-02
553 posts

Could you paste the entire source that’s being outputted ? If it’s not too complex I suggest using the trim() function, but that will most likely mess up any HTML that’s being outputted:

<?php
$var 
"<div id=\"foo\">foo</div>";
echo 
trim($var);
?> 

This will output

<divid="foo">foo</div

Which is incorrect.

 
Posted: 02 May 2009 10:30 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2008-05-18
2398 posts

you mean there’s 2 spaces in the rendered HTML (aka ‘</html>  ‘?)

You could always intercept the output before its sent to the browser, and call trim on it?

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

 
Posted: 02 May 2009 10:30 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2008-05-18
2398 posts

@Yorick Peterse: Assuming PHPs trim is the same as every other languages, it only trims spaces at the begining and end

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

 
Posted: 02 May 2009 10:32 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2009-05-02
553 posts
Dam1an - 02 May 2009 02:30 PM

@Yorick Peterse: Assuming PHPs trim is the same as every other languages, it only trims spaces at the begining and end

Hm..Never knew that actually smile

 
Posted: 02 May 2009 11:25 AM   [ # 5 ]   [ Rating: 0 ]
Joined: 2008-11-08
85 posts

Hi all,

To explain it further:

A third-party app is visiting a method of my controller. This method will authenticate the visit.
The third-party app expects, to have 0 returned for success and anything else is considered failure by the third-party app. So if i output 0 with 2 trailing whitespace like ’  0’, it is considered failure.

I try to use

$data 0;
$this->output->set_output($data); 

but this will generate output ’  0’. Then i check other pages in my CI app (ususally output done by a view) and they all have 2 trailing white spaces.
Trimming will not work because $this->output->set_output($data); has to be the last call and that gives me 2 trailing white spaces

Any ideas?
Michael

 
Posted: 02 May 2009 11:55 AM   [ # 6 ]   [ Rating: 0 ]
Joined: 2008-11-08
85 posts

Hi all,

I found, that even a simple few like:

<?php echo ''?> 

will output 2 white spaces.

Any ideas how to fix that?

Michael

 
Posted: 02 May 2009 12:14 PM   [ # 7 ]   [ Rating: 0 ]
Joined: 2009-05-02
553 posts
<?php
$var 
'  '//Variable with 2 spaces
$var str_replace("  ","",$var); //Replace the 2 spaces with nothing.
echo $var//Should produce nothing (empty string)
?> 
 
Posted: 02 May 2009 12:28 PM   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2009-03-21
680 posts

Micheal,

How are you determining that there are 2 trailing whitespaces?

I did this:

echo 0

and this:

echo 'apples'

each both with and without CodeIgniter and neither time did I see any trailing whitespace.

Are you viewing the output through a browser or it’s source code view?  Is it an AJAX response?  I have a feeling that these trailing spaces are from something else and you don’t need to worry about them.

 
Posted: 02 May 2009 01:35 PM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2008-04-25
4956 posts

I don’t understand. Are these trailing spaces or leading spaces?

It sounds to me like the HTML source in the view has spaces in it. Remove them and everything should be fine. Failing that, it must be something in your code that’s doing it. I’m willing to bet that even an empty controller method will still output two spaces.

 Signature 

http://www.phptherightway.com

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.

 
Posted: 02 May 2009 01:42 PM   [ # 10 ]   [ Rating: 0 ]
Joined: 2008-11-08
85 posts

slowgary,

I determine the 2 white space by selecting the complete output in source view. You will not see them in browser view.
The third-party app visiting expects, to have a 0, 1, 2, 3, 4, 5, 6, 7 returned, as the result of my method. As the third-pasty app fails, i suppose the 2 trailing white spaces are the problem. No AJAX involved, not even a single HTML tag, just these codes. Not more, not less.
That seems to be a small thing, but IMHO this would be a serious flaw. IN ASP, Java or whatever i used in my work before, i had complete control over the output.
What makes things worse, it is not possible to bend the third-party app. So I have to make CI behave correctly… 

Any ideas?

Thanks,
Michael

 
Posted: 02 May 2009 01:46 PM   [ # 11 ]   [ Rating: 0 ]
Joined: 2008-11-08
85 posts

TheFuzzyOne,

Sorry, english is not my mother tongue. I suppose, i meant leading spaces, like ’  0’

Michael

 
Posted: 02 May 2009 02:08 PM   [ # 12 ]   [ Rating: 0 ]
Joined: 2007-03-21
43 posts

Might you try re-downloading and re-installing code igniter? Maybe one of the files has whitespace at the beginning of the file.

Also make sure your view files don’t have any whitespace as well.

 
Posted: 02 May 2009 02:27 PM   [ # 13 ]   [ Rating: 0 ]
Avatar
Joined: 2009-03-21
680 posts

You do have complete control over your output in both PHP and CodeIgniter.  I haven’t seen this problem ever myself, so it sounds like maybe there’s something doing it in your code.

Maybe try posting your controller and view or something.

 
Posted: 02 May 2009 02:40 PM   [ # 14 ]   [ Rating: 0 ]
Joined: 2008-11-08
85 posts

This happens even when i don`t use view files, but use

$this->output->set_output(‘0’);

But if you suspect it is somehow corrupted CI installation, can you try

$this->output->set_output(‘0’);

on your machine and examine the output?

Thanks,
Michael

 
Posted: 02 May 2009 03:00 PM   [ # 15 ]   [ Rating: 0 ]
Joined: 2008-11-08
85 posts

Reinstalling CI (except my application folder) did not fix the issue.

slowgary,

That`s my controller:

<?php
class Users extends Controller { 
  function Users()
  { 
      parent::Controller();
  } 

  function process_chat_login()
  {
      //@TODO implement processing of chat login
      $data = array();
      $data[‘result’] = 1;
      $login = array();
      $login[0] = $_GET[‘username’];
      $login[1] = $_GET[‘password’];
      if($this->auth->process_login($login))
      {
        $data[‘result’] = 0;
      }
      $this->load->view(‘login_chat_view’, $data);
  }

}
?>

And that`s the view:

<?php echo $result?> 

As I already mentioned using

$this->output->set_output($data['result']); 

instead of a view would not help.
Using string literals instead of numbers yielded the same: 2 leading spaces.

Michael

 
1 of 3
1