使用 Twig 检查 url 中的数字的正则表达式
Regex to check for a number in the url using Twig
如果我对 Twig 多了解一点,这就不是什么大问题了。
这是我的代码,我正在尝试检查 url 中是否包含数字。
{% set x = req.path %}
{% set matches = x|regex_match('~[\d\.]+$~') %}
{% set matches = x|regex_match('~[0-9]*$+~') %}
none 这些作品
提前致谢
For complex string comparisons, the matches
operator allows you to use regular expressions:
{% if phone matches '/^[\d\.]+$/' %}
{% endif %}
因此,要检查字符串是否包含带正则表达式的数字,只需使用
{% if x matches '/\d/' %}
Do Stuff
{% endif %}
为什么要为此使用 Twig?使用路由功能应该更可靠?
my_route_to_check:
pattern: /my/route/{id}
defaults: { _controller: acmeMybundle:myController:myAction, id:0}
requirements:
id : \d+
这里的{id}必须是整数(有\d+规则)否则会触发异常
更简单,不是吗?
如果我对 Twig 多了解一点,这就不是什么大问题了。 这是我的代码,我正在尝试检查 url 中是否包含数字。
{% set x = req.path %}
{% set matches = x|regex_match('~[\d\.]+$~') %}
{% set matches = x|regex_match('~[0-9]*$+~') %}
none 这些作品
提前致谢
For complex string comparisons, the
matches
operator allows you to use regular expressions:
{% if phone matches '/^[\d\.]+$/' %}
{% endif %}
因此,要检查字符串是否包含带正则表达式的数字,只需使用
{% if x matches '/\d/' %}
Do Stuff
{% endif %}
为什么要为此使用 Twig?使用路由功能应该更可靠?
my_route_to_check:
pattern: /my/route/{id}
defaults: { _controller: acmeMybundle:myController:myAction, id:0}
requirements:
id : \d+
这里的{id}必须是整数(有\d+规则)否则会触发异常
更简单,不是吗?