问题描述
相当简单的问题,但我是正则表达式和 smarty 模板的新手,我似乎无法弄清楚如何在我的 smarty 模板中使用它.
Fairly simple question, but I am new to regular expressions and smarty templates, and I can't seem to figure out how to get this working in my smarty template.
基本上,我有可以包含字符串值的智能字符串,例如:
Basically, I have smarty string that can contain string values like:
Home Page Layout 1-1
Home Page Layout 1-2
Home Page Layout 2-1
etc..
我只想从字符串中提取整数,以便输出如下:
I want to extract just the integers from the string so that the output is as followed:
11
12
21
如您所见,我正在删除除整数之外的所有内容.我在四处搜索后尝试执行以下操作,但它不起作用或输出任何内容.
As you can see, I'm removing everything but the integers. I tried doing the following after searching around, but it doesn't work or output anything.
{$SelectHomePageLayout|regex_replace:'[^0-9]+':''}
相当简单,但我不确定我做错了什么.
Fairly simple but I'm just not sure what I'm doing wrong.
推荐答案
您的正则表达式缺少正则表达式分隔符(即 //
).使用以下一个,并检查分隔符是如何添加的.
You are missing the regex delimiter(i.e. //
) for your regex. Use the following one, and check how the delimiter is added.
{$SelectHomePageLayout|regex_replace:'/[^0-9]+/':''}
^ ^
这篇关于如何过滤 Smarty 变量以仅输出数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!