php Smarty 以 strpos 为开始,以 strlen 为结束获取 var 的子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4389978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Smarty getting substring of a var with strpos as start and strlen as end
提问by u476945
I am having issue formatting variables in smarty. I was wondering what is the best way to do it. Basically i have a string "ABC | DEFGH" i want smarty to get the substring of "DEFGH" How would i go about doing this?
我在 smarty 中遇到格式化变量的问题。我想知道什么是最好的方法。基本上我有一个字符串“ABC | DEFGH”我想要聪明地获得“DEFGH”的子字符串我将如何去做?
{$var|substr:strpos:"|":strlen}
doesn't work
不起作用
回答by Jimby
Just solved this without setting var back in PHP, and by using the built-in function wrappers.
刚刚解决了这个问题,无需在 PHP 中重新设置 var,并使用内置函数包装器。
Assuming that: $var = "ABC|DEFGH";
假设: $var = "ABC|DEFGH";
{assign var="bar_at" value=$var|strpos:"|"}
<li>{$var}</li>
<li>{$var|substr:0:$bar_at}</li>
<li>{$var|substr:$bar_at+1}</li>
This will print:
这将打印:
回答by Marcin Nabia?ek
Some info about Smarty 3 in case if someone want to achieve the same in Smarty 3 and not in Smarty 2:
关于 Smarty 3 的一些信息,以防有人想在 Smarty 3 而不是在 Smarty 2 中实现相同的目标:
The first thing is that you need to add parentheses to make it work. substr:$bar_at+1
won't work but substr:($bar_at+1)
will work.
第一件事是您需要添加括号以使其工作。substr:$bar_at+1
不会工作,但substr:($bar_at+1)
会工作。
But in fact you can use simpler syntax:
但实际上你可以使用更简单的语法:
{assign var="var" value="ABC | DEFGH"}
{$var|substr:($var|strpos:"|"+1)}
This will give you DEFGH
with space at the beginning (before and |
are spaces inside var in your question and in my example) and as you want to get string without space you should use in this case +2
instead of +1
:
这将DEFGH
在开头为您提供空间(|
在您的问题和我的示例中,在 var之前和是空格),并且由于您想获得没有空格的字符串,因此您应该在这种情况下使用+2
而不是+1
:
{$var|substr:($var|strpos:"|"+2)}
Those above were tested in Smarty 3.1.19
.
以上这些都在 Smarty 中进行了测试3.1.19
。
You should also know that in previous versions (before Smarty 3.1) if you have in your string UTF-8 characters you should rather use mb_
functions.
您还应该知道,在以前的版本中(Smarty 3.1 之前),如果您的字符串中有 UTF-8 字符,您应该使用mb_
函数。
As of Smarty 3.1 in case if mbstring
extension is installed Smarty automatically uses mb_
functions in that case so there won't be any problem with utf-8 characters.
从 Smarty 3.1 开始,如果mbstring
安装了扩展,Smartymb_
在这种情况下会自动使用函数,因此 utf-8 字符不会有任何问题。
You can read more about Smarty utf-8 encoding
您可以阅读有关Smarty utf-8 编码的更多信息
回答by profitphp
Those functions do not exist in smarty. You'll have to split it in PHP before sending it to the template. Or you could write your own split function smarty plugin. Or use {php} tags in the template, but I'd avoid that solution as it is against the 'spirit' and purpose of using smarty, separation of presentation and logic, etc etc.
smarty中不存在这些功能。在将其发送到模板之前,您必须将其拆分为 PHP。或者您可以编写自己的拆分功能 smarty 插件。或者在模板中使用 {php} 标签,但我会避免使用这种解决方案,因为它违背了使用 smarty、演示和逻辑分离等的“精神”和目的。
回答by inMILD
When using like Jimby:
像 Jimby 一样使用时:
{$var|substr:$bar_at+1}
I add parentheses or round bracket to make it work.
我添加了括号或圆括号以使其工作。
{$var|substr:($bar_at+1)}