Search code examples
phphtmlurlsmarty

Smarty remove everything after last character


I have a Smarty variable thats called{$url} and it contains following 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?

Thanks for your help!


Solution

  • Try with explode,array_slice and implode
    Like this :-

    $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

    $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