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
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@jadelius.se:
You just need to remove the newline between
#!/usr/bin/php
and
<?php
Björn Jadelius
textmate mailing list textmate@lists.macromates.com http://lists.macromates.com/listinfo/textmate
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.
Good advise Rob. Thanks.
Ryan
On Fri, Dec 11, 2009 at 8:35 AM, Rob McBroom mailinglist0@skurfer.com 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@lists.macromates.com http://lists.macromates.com/listinfo/textmate