php Smarty 如果 URL 包含
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8280984/
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 If URL Contains
提问by 99823
Using Smarty Tags I'd like to determine if an URL contains a word, something like:
使用 Smarty 标签我想确定一个 URL 是否包含一个单词,例如:
{if $smarty.get.page contains "product.php"} .....
I know contains doesn't exist, but how could I easily go about writing something similar to achieve the above code?
我知道 contains 不存在,但是我如何轻松地编写类似的东西来实现上述代码?
采纳答案by tekknolagi
You can use strpos
to check if a string has another string inside of it.
你可以strpos
用来检查一个字符串里面是否有另一个字符串。
$pos = strpos($smarty.get.page, "product.php");
if($pos !== false) {
// found, do something
}
Except you need to wrap it in {php}
and {/php}
.
除非您需要将它包装在{php}
and 中{/php}
。
$smarty.get.page
translates to $_GET['page']
, so you could replace it with the GET variable as well.
$smarty.get.page
转换为$_GET['page']
,因此您也可以将其替换为 GET 变量。
回答by meze
All PHP conditionals and functions are recognized, such as ||, or, &&, and, is_array(), etc.
所有 PHP 条件和函数都被识别,例如 ||、or、&&、and、is_array() 等。
{if strpos($smarty.get.page, "product.php") !== false}
{if strpos($smarty.get.page, "product.php") !== false}