Unwanted newline php

View: New views
6 Messages — Rating Filter:   Alert me  

Unwanted newline php

by ryan fitzer :: Rate this Message:

| View Threaded | Show Only this Message

I'm writing a command using php that reorders a css property list by
string length. The problem I'm having is that a newline character is
being added at the beginning of the result for some reason (using
Replace selected text). I've consulted this thread
http://lists.macromates.com/textmate/2007-April/019068.html but I've
found no way of echoing a string without the newline being added. Here
is the command:

#!/usr/bin/php

<?php

$fstat = fstat(STDIN);
$stdin = fread(STDIN, $fstat['size']);

//print_r($stdin);

$pattern = '/\{(.*)\}/Us';
preg_match($pattern, $stdin, $matches);
$output = trim($matches[1], "\n");
$output = preg_split('/\n/', $output);

function sort_strlength($val_1, $val_2) {
        // initialize the return value to zero
        $retVal = 0;

        // compare lengths
        $firstVal = strlen($val_1);
        $secondVal = strlen($val_2);

        if($firstVal > $secondVal) {
                $retVal = 1;
        } else if($firstVal < $secondVal) {
                $retVal = -1;
        }
       
        return $retVal;
}

uasort($output, "sort_strlength");

$out = '{' . "\n";
foreach($output as $line) {
        $out .= $line . "\n";
}
$out .= '}';

print(trim($out));

?>

Any help would be appreciated.

Ryan

--
Ryan Fitzer
Los Angeles, CA
http://www.ryanfitzer.com
http://www.portfoliorodeo.com

_______________________________________________
textmate mailing list
textmate@...
http://lists.macromates.com/listinfo/textmate

Re: Unwanted newline php

by Björn Jadelius :: Rate this Message:

| View Threaded | Show Only this Message

You just need to remove the newline between

> #!/usr/bin/php
and
> <?php

Björn Jadelius

_______________________________________________
textmate mailing list
textmate@...
http://lists.macromates.com/listinfo/textmate

Re: Unwanted newline php

by ryan fitzer :: Rate this Message:

| View Threaded | Show Only this Message

Holy cow! I can't believe I missed that. Of course, I new to server
side languages. Thanks so much for the help.

Here is the updated command for those interested (a little more efficient)

-----------------------
#!/usr/bin/php
<?php

$fstat = fstat(STDIN);
$stdin = fread(STDIN, $fstat['size']);

$pattern = '/\{(.*)\}/Us';
preg_match($pattern, $stdin, $matches);
$match = trim($matches[1], "\n");
$match = preg_split('/\n/', $match);

$properties = array();

foreach($match as $line) {
        $properties[strlen($line)] = $line;
}

ksort($properties);

$output = '{' . "\n";
foreach($properties as $line) {
        $output .= $line . "\n";
}
$output .= '}';

echo(trim($output));

?>
-----------------------

Ryan

2009/12/10 Björn Jadelius <b@...>:

> You just need to remove the newline between
>
>> #!/usr/bin/php
> and
>> <?php
>
> Björn Jadelius
>
> _______________________________________________
> textmate mailing list
> textmate@...
> http://lists.macromates.com/listinfo/textmate
>



--
Ryan Fitzer
Los Angeles, CA
http://www.ryanfitzer.com
http://www.portfoliorodeo.com

_______________________________________________
textmate mailing list
textmate@...
http://lists.macromates.com/listinfo/textmate

Re: Unwanted newline php

by Rob McBroom-4 :: Rate this Message:

| View Threaded | Show Only this Message

On Dec 11, 2009, at 2:41 AM, Ryan Fitzer wrote:

> Holy cow! I can't believe I missed that. Of course, I new to server
> side languages.

Just remember that PHP will treat everything outside of <?php ?> as part of a document and blindly output it.

This has bitten many people when they try to send a Content-type header and get errors because their script started with blank lines and/or comments, which causes most web servers to start sending it as “text/html” automatically.

--
Rob McBroom
<http://www.skurfer.com/>


_______________________________________________
textmate mailing list
textmate@...
http://lists.macromates.com/listinfo/textmate

Re: Unwanted newline php

by ryan fitzer :: Rate this Message:

| View Threaded | Show Only this Message

Good advise Rob. Thanks.

Ryan

On Fri, Dec 11, 2009 at 8:35 AM, Rob McBroom <mailinglist0@...> wrote:

> On Dec 11, 2009, at 2:41 AM, Ryan Fitzer wrote:
>
>> Holy cow! I can't believe I missed that. Of course, I new to server
>> side languages.
>
> Just remember that PHP will treat everything outside of <?php ?> as part of a document and blindly output it.
>
> This has bitten many people when they try to send a Content-type header and get errors because their script started with blank lines and/or comments, which causes most web servers to start sending it as “text/html” automatically.
>
> --
> Rob McBroom
> <http://www.skurfer.com/>
>
>
> _______________________________________________
> textmate mailing list
> textmate@...
> http://lists.macromates.com/listinfo/textmate
>



--
Ryan Fitzer
Los Angeles, CA
http://www.ryanfitzer.com
http://www.portfoliorodeo.com

_______________________________________________
textmate mailing list
textmate@...
http://lists.macromates.com/listinfo/textmate

Re: Unwanted newline php

by Allan Odgaard-4 :: Rate this Message:

| View Threaded | Show Only this Message

On 11 Dec 2009, at  8:41, Ryan Fitzer wrote:

> $fstat = fstat(STDIN);
> $stdin = fread(STDIN, $fstat['size']);

I am surprised this works. STDIN is a pipe and can’t be stat’ed for
file size.

I would suggest instead using file_get_contents("php://stdin").

_______________________________________________
textmate mailing list
textmate@...
http://lists.macromates.com/listinfo/textmate