Is there an equivalent to Perl's format function in PHP? I have a client that has an old-ass okidata dotmatrix printer, and need a good way to format receipts and bills with this arcane beast.
I remember easily doing this in perl with something like:
format BILLFORMAT =
Name: @>>>>>>>>>>>>>>>>>>>>>> Age: @###
$name, $age
.
write;
Any ideas would be much appreciated, banging my head on the wall with this one. O.o
UPDATE: I cannot install Perl in this environment, o开发者_运维技巧therwise I would simply use Perl's format function directly.
You could use printf to do something similar.
http://www.php.net/manual/en/function.printf.php
printf("Name: %21s Age: %3i\n",$name,$age);
If you wanted the name left aligned, you would just add a -
printf("Name: %-21s Age: %3i\n",$name,$age);
It defaults to right aligned.
If you don't mind using a Perl process to control the printer, you could serialize the data in PHP and pass it to a Perl script.
I've had great luck using PHP::Serialization to handle data serialization and sharing between Perl and PHP. You could also use YAML or JSON for this task.
Sounds like a perfect situation to use heredoc.
精彩评论