Harry Beasant Harry Beasant - 7 months ago 23
PHP Question

Removing last part of string divided by a colon

I have a string that looks a little like this,

world:region:bash


It divides folder names, so i can create a path for FTP functions.

However, i need at some points to be able to remove the last part of the string, so, for example

I have this
world:region:bash


I need to get this
world:region


The script wont be able to know what the folder names are, so some how it needs to be able to remove the string after the last colon.

Answer
$res=substr($input,0,strrpos($input,':'));

I should probably highlight that strrpos not strpos finds last occurrence of a substring in given string

Comments