trim wont work since it only removes whitespaces but not slashes. U might use something like this to simply leave out the first char:
$path_elements['dirname'] = substr($path_elements['dirname'], 1, strlen($path_elements['dirname'] - 1);
before u echo the value