views:

71

answers:

3

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, otherwise I would simply use Perl's format function directly.

+3  A: 

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.

Joshua Smith
But that's quite different than perl's `format`.
VolkerK
That's great for simply replacing variables, but it does not allow the flexibility for aligning rows and iterating through arrays such as the perl format function does. ;-)
Dustin Hansen
While it is quite different from perl's format function it does allow for alignment and iteration (quite easily). printf was born to do what he's talking about doing: formatting output for fixed width devices.
Joshua Smith
I will do more research in to printf to see if there's anything it is missing from the format function then, thanks Joshua.
Dustin Hansen
But `format` can do so much more. There's `format xyz_TOP`. There's the ability to apply the format to a stream somewhere, then prepare the data elsewhere and simply `write`. Run code in the format. Cut off (and reuse) data that's too long for a field and so on and on... There's a lot to code when trying to replace it by printf.
VolkerK
Agreed, volkerK, that's what I need to look into as far as what I need for this instance. :(
Dustin Hansen
@VolkerK I agree, format has _way_ more features.
Joshua Smith
A: 

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.

daotoad
If I could use perl, I would just use the Perl format and read the data I need directly from the database though. Sorry, I should have stated that up front, I guess.
Dustin Hansen
@Dustin, I figured there was a good chance it wouldn't work out since you asked for a PHP solution. My response may fit someone else's needs.
daotoad
@daotoad indeed. ;-)
Dustin Hansen
A: 

Sounds like a perfect situation to use heredoc.

Sonny
Heredoc also falls quite short of the format functionality. Again, lacks array iteration, vertical and horizontal alignment, etc.
Dustin Hansen
I was unaware that `format` provided iteration. The vertical alignment could easily be accomplished with the `str_pad` function. Based on feedback, it looks like `printf` is your best bet anyway.
Sonny
Ouch, I'm picking up some hate here.
Sonny