tiny raw strings formatting engine for PHP
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Pluto - tiny string formatting engine for PHP

Pluto was designed to port Python-like string formatting (with named subpatterns) to PHP. Actually, I went a little beyond and added modifiers/ simple operators (like array->string imploding and 'default' modifier).

Further work

  • Some fixes & replacing ternary operators with some flexible way of representing processing flow
  • More operators (NB: Pluto is not a template engine, thus it shouldn't store complex logic; only simpliest things like modifier)
  • Custom (user-defined) operators & modifiers
  • Custom callbacks for core functional
  • Error managing (e.g., passing map of cases and associated Exception subclasses)
  • Possible: returning object of class FormattedString, not just of built-in string type. FormattedString keeps the raw string and results of processing; __toString returns actual formatted string.



$greeting = string("Hello, %(name)s!", array('name'=>'Octocat')); // Hello, Octocat!

Default value & array->string:

$url = string("%(protocol|default=http)s://%(site)s/%(section)s/%(slugs){[/]*}", 
          array('site'=>'mysite.com', 'section'=>'blog', 'slugs'=>
             array('this', 'is', 'post')));
// http://mysite.com/blog/this/is/post

Array->string & pruning:

$pruned_path = string("%(folders){[/]3}", array('folders'=>array('my', 'list', 'of', 'folders'))) 
// my/list/of


$ip = string("%(\0).%(\1).%(\2).%(\3)", array(192, 168, 0, 1));


$my_percents = string("\%(my) %(percents)", array('percents'=>'80%', 'my'=>'your'));
// %(my) 80%

Array->string with custom glue:

$socket = string("%(parts){[:]*}", array('parts'=>array('', '8080')));