Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Smarty-如何将字符串的一部分从url存储到变量?_Php_Html_Smarty - Fatal编程技术网

Php Smarty-如何将字符串的一部分从url存储到变量?

Php Smarty-如何将字符串的一部分从url存储到变量?,php,html,smarty,Php,Html,Smarty,我不知道,在网上也找不到如何存储函数分解为变量的结果。我做错了什么?请求URI为:/cz/cs/15_test.html。15_test.html是我需要存储在变量中的内容。非常感谢你的帮助 SMARTY: {assign var="url_catname" value=explode("/",$smarty.server.REQUEST_URI)} {assign var="url_catname" value=$url_catname[3]} 不能像这样从Smarty调用PHP函数。从PH

我不知道,在网上也找不到如何存储函数分解为变量的结果。我做错了什么?请求URI为:/cz/cs/15_test.html。15_test.html是我需要存储在变量中的内容。非常感谢你的帮助

SMARTY:

{assign var="url_catname" value=explode("/",$smarty.server.REQUEST_URI)}
{assign var="url_catname" value=$url_catname[3]}

不能像这样从Smarty调用PHP函数。从PHP代码执行赋值:

$smarty = new Smarty();
$pieces = explode("/", $_SERVER['REQUEST_URI']);
$smarty->assign('url_catname', $pieces[3]);
$smarty->display('blah-blah.tpl');

前面的答案并不完全正确。您可以调用PHP函数,但通常在PHP脚本中执行许多操作更合理,而在Smarty中只显示结果

运行时:

{assign var="url_catname" value=explode("/",'/cz/cs/15_test.html')}
{assign var="url_catname" value=$url_catname[3]}
{$url_catname}
作为您收到的输出:

15_test.html

因此,它似乎可以正常工作(当然,当您使用
$smarty.server.REQUEST\u URI
时也是如此)

以这种方式使用
explode
时将获得完全相同的效果:

{assign var="url_catname" value="/"|explode:'/cz/cs/15_test.html'}
{assign var="url_catname" value=$url_catname[3]}
{$url_catname}
作为您收到的输出:

15_test.html