php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #43908 Add an optional parameter to exec() to not trim whitespace from output
Submitted: 2008-01-22 16:51 UTC Modified: 2012-12-13 00:11 UTC
Votes:18
Avg. Score:3.4 ± 0.8
Reproduced:4 of 6 (66.7%)
Same Version:1 (25.0%)
Same OS:1 (25.0%)
From: Arne dot Heizmann at csr dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.2.5 OS: Windows 2000
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: Arne dot Heizmann at csr dot com
New email:
PHP Version: OS:

 

 [2008-01-22 16:51 UTC] Arne dot Heizmann at csr dot com
Description:
------------
The exec() call is supposed to return the output of the system call into the second parameter.

However, it modifies this output. It behaves as if rtrim() is called on each line. This causes data loss because rtrim() is not reversible.

Reproduce code:
---------------
<?
    header ('Content-type: text/plain');
    file_put_contents ('temp', " "); // also reproducible with "\t"
    exec ("type temp", $ops, $result);
    echo var_export ($ops, true);
?>

Expected result:
----------------
array (
  0 => ' ',
)

Actual result:
--------------
array (
  0 => '',
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-24 02:53 UTC] davidc@php.net
Even easier to reproduce:

<?php var_dump(exec("echo ' '")); ?>
 [2008-01-24 15:46 UTC] Arne dot Heizmann at csr dot com
In your patch it appears that you are still deliberately removing whitespace. Why?
 [2008-01-24 20:37 UTC] felipe@php.net
The actual implementation strip trailing whitespaces, my patch also do it, but when the string contains only whitespaces, it remove just \r and \n.
 [2008-01-25 17:59 UTC] Arne dot Heizmann at csr dot com
> The actual implementation strip trailing whitespaces, my patch also do
> it, but when the string contains only whitespaces, it remove just \r
> and \n.

So why are you explicitly fixing only half of the bug and leaving the other half in?
 [2008-01-25 18:24 UTC] felipe@php.net
Because that is intentional, however, it affect results as your case, when the string is consisting entirely of whitespace.

Well, anyway, maybe this isn't a good idea, it was only my idea.
 [2008-06-02 13:26 UTC] Arne dot Heizmann at csr dot com
Could we have this fix in the next version please? Just remove all the code that removes the whitespace?
 [2008-07-17 23:43 UTC] jani@php.net
Expected behaviour and can not be changed. RTFM: http://php.net/exec
 [2008-07-18 14:28 UTC] Arne dot Heizmann at csr dot com
This is most certainly not expected behaviour, and furthermore, it is dataloss.

If you must preserve backwards compatibility, which is certainly fair, then I recommend you add a new boolean option to the exec() call to preserve the whitespace. Either way, this bug is not bogus.

I have an application whose output may or may not contain lines that contain only whitespace. I need for this whitespace to be preserved as otherwise I cannot parse the output correctly.

Please note that I'm not talking about the trailing "\n" which is mentioned in the documentation. I'm referring to all other whitespace, especially spaces and tabs.
 [2008-07-18 16:02 UTC] jani@php.net
Reclassified.
 [2012-12-12 23:08 UTC] dwake at box dot com
I've just run into this "feature", writing a tool that needs to be able to read in a file from a given commit repo.  Because of the "feature", every line in the resulting file is stripped of trailing whitespace, creating a lot of spurious diffs.

I am at a loss to see the motivation for this behavior.  Information is being discarded, with no possibility of recovering it.  The only workaround I can think of is to
1. Use sed, or a comparable utility, to add a non-whitespace character to the end of every line
2. Make the PHP exec() call
3. In PHP, strip the spurious character from the end of each line.

Please put in the extra flag to turn this behavior off.
 [2012-12-12 23:18 UTC] rasmus@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2012-12-12 23:18 UTC] rasmus@php.net
If you want the raw unaltered output from the command use shell_exec() not 
exec(). By definition exec() has to use a delimiter in order to create the array. 
If you don't want it to do that, just grab the raw via shell_exec() and do your 
own parsing.
 [2012-12-12 23:24 UTC] dwake at box dot com
If I use shell_exec(), is there any way to get the exit code?
 [2012-12-12 23:30 UTC] dwake at box dot com
With regard to the need for a delimiter, I guess that, like Arne, I'm wondering why we couldn't just use the newline character for that purpose.
 [2012-12-13 00:11 UTC] rasmus@php.net
If you want full control, use proc_open/proc_close. proc_close() will give you 
the exit code or you can write $? to one of the streams to get a more accurate 
one. I suppose you could also do something intelligent with $? on the 
shell_exec() command, but that is more of a hack. All these exec() functions 
simply apply different levels of filtering and usability on top of the low-level 
proc_open() call. If it isn't doing what you want drop down a level and implement 
what you need. There are plenty of cases where you want \r\n and all other 
whitespace stripped from the end of lines.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 22:01:30 2024 UTC