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

时间:2022-09-07 20:24:09

I have a Smarty variable thats called{$url} and it contains following URL:

我有一个名为{$ url}的Smarty变量,它包含以下URL:

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

I want everything after the last ":" to be removed.

我希望删除最后一个“:”之后的所有内容。

The result should look like this:

结果应如下所示:

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

With my knowledge it was only possible for me that I get the following result:

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

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

How can I get this result with Smarty?

如何通过Smarty获得此结果?

Thanks for your help!

谢谢你的帮助!

2 个解决方案

#1


0  

Try with explode,array_slice and implode
Like this :-

尝试使用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

Modified:-
For removing everything after the last ":" use array_pop
Like this

修改: - 用于删除最后一个“:”之后的所有内容使用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

#2


1  

i trested it and work :

我感兴趣并且工作:

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

#1


0  

Try with explode,array_slice and implode
Like this :-

尝试使用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

Modified:-
For removing everything after the last ":" use array_pop
Like this

修改: - 用于删除最后一个“:”之后的所有内容使用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

#2


1  

i trested it and work :

我感兴趣并且工作:

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