Does PHP have a feature like Pythons template strings?

Does PHP have a feature like Pythons template strings?

You could also use strtr:

$template = $who likes $what;

$vars = array(
  $who => tim,
  $what => kung pao,
);

echo strtr($template, $vars);

Outputs:

tim likes kung pao

You can use template strings like this:

$name = Maria;
$info[last_name] = Warner;

echo Hello {$name} {$info[last_name]};

This will echo Hello Maria Warner.

Does PHP have a feature like Pythons template strings?

I think there are a bunch of ways to do this… but this comes to mind.

$search = array(%who%, %what_id%);
$replace = array(tim, kung pao);
$conference_target = str_replace(
    $search,
    $replace,
    %who% likes %what%
);

Ha, we even had one in our framework using vsprintf:

class Helper_StringFormat {

    public static function sprintf($format, array $args = array()) {

        $arg_nums = array_slice(array_flip(array_keys(array(0 => 0) + $args)), 1);

        for ($pos = 0; preg_match(/(?<=%)(([a-zA-Z_]w*))/, $format, $match, PREG_OFFSET_CAPTURE, $pos);) {
            $arg_pos = $match[0][2];
            $arg_len = strlen($match[0][0]);
            $arg_key = $match[1][0];

            if (! array_key_exists($arg_key, $arg_nums)) {
                user_error(sprintfn(): Missing argument ${arg_key}, E_USER_WARNING);
                return false;
            }
            $format = substr_replace($format, $replace = $arg_nums[$arg_key] . $, $arg_pos, $arg_len);
            $pos = $arg_pos + strlen($replace);
        }

        return vsprintf($format, array_values($args));
    }
}

Which looks like it came from the sprintf page

This allows for calls like:

sprintfn(second: %(second)s ; first: %(first)s, array(
    first => 1st,
    second=> 2nd
));

UPDATE
Here is an update to do what you want… not fully tested though

class Helper_StringFormat {

    public static function sprintf($format, array $args = array()) {
        $arg_nums = array_slice(array_flip(array_keys(array(0 => 0) + $args)), 1);

        for ($pos = 0; preg_match(/(?<=%)(([a-zA-Z_][ws]*))/, $format, $match, PREG_OFFSET_CAPTURE, $pos);) {
            $arg_pos = $match[0][1];
            $arg_len = strlen($match[0][0]);
            $arg_key = $match[1][0];

            if (! array_key_exists($arg_key, $arg_nums)) {
                user_error(sprintfn(): Missing argument ${arg_key}, E_USER_WARNING);
                return false;
            }
            $format = substr_replace($format, $replace = $arg_nums[$arg_key] . $, $arg_pos, $arg_len);
            $pos = $arg_pos + strlen($replace); // skip to end of replacement for next iteration
        }

        return vsprintf($format, array_values($args));
    }
}

$str = %(my var)s now work with a slight %(my var2)s;
$repl = array(my var => Spaces, my var2 => modification.);

echo Helper_StringFormat::sprintf($str, $repl);

OUTPUT
Spaces now work with a slight modification.

Leave a Reply

Your email address will not be published. Required fields are marked *