PHP是否具有Python的模板字符串之类的功能?
Python的功能称为模板字符串。
>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
我知道PHP允许您写作:
"Hello $person"
$ Person
替换,但是模板可以在代码的各个部分中重复使用吗?
Python has a feature called template strings.
>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
I know that PHP allows you to write:
"Hello $person"
and have $person
substituted, but the templates can be reused in various sections of the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
另一个更简单的方法是:
是的,PhpStorm会抱怨...
Another more simple approach would be this:
And yes, PHPStorm will complain...
我个人最喜欢 sprintf (或 vsprintf ,对于一系列参数)。它以预期的顺序将它们放置,根据需要胁迫类型,并具有更多高级功能。
示例:
这将导致价值
cookies的成本1.10美元
。有一个用于不同用例的printf功能的全家函数,所有这些功能都列在“参见”下。
非常通用:提供变量,数组组件,功能结果等的相同方法。
I personally most like sprintf (or vsprintf, for an array of arguments). It places them in the intended order, coerces types as needed, and has a lot more advanced features available.
Example:
This will result in the value
Cookies cost 1.10 dollars
.There's an entire family of printf functions for different use cases, all listed under "See Also".
Very versatile: same methods for providing variables, array components, function results, etc.
我做了一个工作来做你想做的事。我之所以成为“ Quck and-dirty”,是因为我没有太多时间来重构它,也许我将其上传到了我的github。
编辑:错误校正...
将其像
变量一样使用,只能是[A-ZA-Z0-9_]。
I made a function to do what you want. i made it "quck-and-dirty" because i have not much time to refactorize it, maybe i upload it to my github.
EDIT: a bug correction...
Use it like
Variables can be [a-zA-Z0-9_] only.
只是为了完整的目的:还有 Heredoc 。
输出:
Sidenotes:
{$ data ['Who'']}
的逃脱数组。访问$ data-> Who
无括号的objekts。fn($ a)=> $ a
自PHP 7.4以来可用。您可以编写函数($ a){返回$ a;}
,如果您使用的是php< 7.4。Just for the sake of completeness: there is also Heredoc.
Outputs:
Sidenotes:
{$data['who']}
. Accessing objekts like$data->who
works without braces.fn($a)=>$a
are available since PHP 7.4. You can writefunction($a){return $a;}
if you are using PHP<7.4.另一个选择是将消息形式用作功能或对象:
输出:
An other option is to use MessageFormatter as function or object:
Output:
您可以使用这样的模板字符串:
这将回荡
您好Maria Warner
。You can use template strings like this:
This will echo
Hello Maria Warner
.您也可以使用
strtr
:输出:输出:
You could also use
strtr
:Outputs:
我认为有很多方法可以做到这一点...但这想到了。
,我们在框架中甚至使用 vsprintf :
输出
, $arg_pos, $arg_len); $pos = $arg_pos + strlen($replace); } return vsprintf($format, array_values($args)); } }尚未完全测试
现在空间进行稍作修改。
哈 它来自 sprintf页面
=“ http://www.php.net/manual/fr/function.sprintf.php#94608 ”
href 强>
这是要完成您想要的更新...尽管
输出
尚未完全测试
现在空间进行稍作修改。
I think there are a bunch of ways to do this... but this comes to mind.
Ha, we even had one in our framework using vsprintf:
OUTPUT
, $arg_pos, $arg_len); $pos = $arg_pos + strlen($replace); } return vsprintf($format, array_values($args)); } }Spaces now work with a slight modification.
Which looks like it came from the sprintf page
This allows for calls like:
UPDATE
Here is an update to do what you want... not fully tested though
OUTPUT
Spaces now work with a slight modification.