Smarty删除最后一个角色后的所有内容

时间:2017-07-31 10:56:31

标签: php html url smarty

我有一个叫做{$url}的Smarty变量,它包含以下网址:

$url = https://www.example.com/?parameter=hello:world:itsme

我想要在最后一次之后的所有事情":"被删除。

结果应该如下所示:

$result = https://www.example.com/?parameter=hello:world

据我所知,我只能得到以下结果:

$result = https://www.example.com/?parameter=hello

如何使用Smarty获得此结果?

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

我感兴趣并且工作:

<?php
$url = "https://www.example.com/?parameter=hello:world:itsme";
$i=7;
while($url[$i]!=':') $i++;
echo (substr($url,0,$i));
?>       

答案 1 :(得分:0)

尝试 explode,array_slice和implode
像这样: -

$url = 'https://www.example.com/?parameter=hello:world:itsme';
$array=explode(':',$url);
$array=array_slice($array, 0, 3);
$url=implode(':',$array);
https://www.example.com/?parameter=hello:world //as o/p

<强>修饰: -
在最后一次&#34;之后删除所有内容:&#34;使用 array_pop
喜欢这个

$url = 'https://www.example.com/?parameter=hello:world:itsme:itsme2';
$array=explode(':',$url);
array_pop($array);
$url=implode(':',$array);
https://www.example.com/?parameter=hello:world:itsme //as o/p