Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tiny raw strings formatting engine for PHP
PHP
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
README.markdown
string.php

README.markdown

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.

Examples

Basic:

$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

References:

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

Escaping:

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

Array->string with custom glue:

$socket = string("%(parts){[:]*}", array('parts'=>array('192.168.0.1', '8080')));
// 192.168.1.10:8080
Something went wrong with that request. Please try again.