Search code examples
perlformat

PHP equivalent to Perl format function


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.


Solution

  • 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.