带有命名参数的 vsprintf 或 sprintf,或 PHP 中的简单模板解析 [英] vsprintf or sprintf with named arguments, or simple template parsing in PHP

查看:28
本文介绍了带有命名参数的 vsprintf 或 sprintf,或 PHP 中的简单模板解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来为 sprintfprintf 使用命名参数.

I'm searching for a way to use named arguments for sprintf or printf.

示例:

sprintf(
  'Last time logged in was %hours hours, 
   %minutes minutes, %seconds seconds ago'
  ,$hours,$minutes, $seconds
);

或通过 vsprintf 和关联数组.

or via vsprintf and an associative array.

我在这里找到了一些编码示例

I have found some coding examples here

function sprintfn ($format, array $args = array())

http://php.net/manual/de/function.sprintf.php

这里

function vnsprintf( $format, array $data)

http://php.net/manual/de/function.vsprintf.php

人们在哪里编写自己的解决方案.

where people wrote their own solutions.

但我的问题是,是否有标准的 PHP 解决方案可以实现这一点,或者是否有另一种方式,也许使用 PEAR 提供的简单 PHP 模板,我可以通过坚持标准 PHP 来实现这一点?

But my question is, is there maybe an standard PHP solution out there to achieve this or is there another way, maybe with a simple PHP templating provided by PEAR, that I can achieve this by sticking to standard PHP?

感谢您的帮助.

推荐答案

据我所知 printf/sprintf 不接受 assoc 数组.

As far as I know printf/sprintf does not accept assoc arrays.

但是可以这样做 printf('%1$d %1$d', 1);

总比没有好;)

这篇关于带有命名参数的 vsprintf 或 sprintf,或 PHP 中的简单模板解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆