带有命名参数的 vsprintf 或 sprintf,或者 PHP 中的简单模板解析
我正在寻找一种为 sprintf
或 printf
使用命名参数的方法。
示例:
sprintf(
'Last time logged in was %hours hours,
%minutes minutes, %seconds seconds ago'
,$hours,$minutes, $seconds
);
或通过 vsprintf
和关联数组。
我在这里找到了一些编码示例
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
人们编写了自己的解决方案。
有没有内置的 PHP 函数可以实现这一点?
I'm searching for a way to use named arguments for sprintf
or printf
.
Example:
sprintf(
'Last time logged in was %hours hours,
%minutes minutes, %seconds seconds ago'
,$hours,$minutes, $seconds
);
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
and here
function vnsprintf( $format, array $data)
http://php.net/manual/de/function.vsprintf.php
where people wrote their own solutions.
Is there a built-in PHP function to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
迟到了,但您可以简单地使用 strtr 来“翻译字符”或替换子字符串”
输出以下内容:
Late to the party, but you can simply use strtr to "translate characters or replace substrings"
outputs the following:
我专门为这个需要编写了一个小组件。它称为 StringTemplate。
有了它,您可以通过如下代码获得您想要的东西:
希望能有所帮助。
I've written a small component exactly for this need. It's called StringTemplate.
With it you can get what you want with a code like this:
Hope that can help.
据我所知 printf/sprintf 不接受 assoc 数组。
然而,可以做
printf('%1$d %1$d', 1);
总比什么都不做好;)
As far as I know printf/sprintf does not accept assoc arrays.
However it is possible to do
printf('%1$d %1$d', 1);
Better than nothing ;)
这是来自 php.net
示例:
示例 2:
This is from php.net
Example:
Example 2:
我知道这个问题已经解决了太久了,但也许我的解决方案足够简单,但对其他人有用。
通过这个小功能,您可以模仿一个简单的模板系统:
像这样使用它:
这将输出:
也许不适合每个人和每种情况,但它救了我。
I know this has been resolved for too long now, but maybe my solution is simple enough, yet useful for somebody else.
With this little function you can mimic a simple templating system:
Use it like this:
This would output:
Maybe not for everyone and every case, but it saved me.
请参阅 drupal 的实现
https://api.drupal.org/ api/drupal/includes%21bootstrap.inc/function/format_string/7
很简单,不使用正则
表达式示例:
See drupal's implementation
https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/format_string/7
It's simple and doesn't use regexp
Example:
从5.3开始,因为
use
关键字:此函数支持格式化{{var}}或{{dict.key}},您可以更改
{{} }
到{}
等以符合您的喜好。示例:
输出:
这是吉姆,我今年 20 岁,我有一只猫叫黄。
Since 5.3 because of the
use
keyword:This function supports formatting {{var}} or {{dict.key}}, you can change the
{{}}
to{}
etc to match you favor.Example:
Output:
This is Jim, I am 20 years old, I have a cat called huang.
这就是我正在使用的:
This is what I'm using:
恕我直言,这确实是最好的方式。没有神秘字符,只需使用键名即可!
取自 php 站点:
http://www.php.net/manual/en /function.vsprintf.php
This is really the best way to go imho. No cryptic characters, just use the key names!
As taken from the php site:
http://www.php.net/manual/en/function.vsprintf.php
您需要避免在自定义函数中使用 %,因为它可能会干扰其他实现,例如 SQL 中的日期格式,所以...
$string1: Mary 有一只小羊羔。它的羊毛洁白如雪。
$string2:玛丽有一只小羊羔。它的羊毛洁白如雪。
You'll want to avoid using % in you custom functions as it can interfere with other implementations, for example, date formatting in SQL, so...
$string1: Mary had a little lamb. Its fleece was white as snow.
$string2: Mary had a little lamb. Its fleece was white as snow.