Das PHP Script Verzeichnis

Diese kleine Routine fügt am Ende eines Pfades einen Slash hinzu.

function add_ending_slash($path){
$slash_type = (strpos($path, '\')===0) ? 'win' : 'unix';
$last_char = substr($path, strlen($path)-1, 1);
if ($last_char != '/' and $last_char != '\') {
// no slash:
$path .= ($slash_type == 'win') ? '\' : '/';
}
return $path;
}

Alternative

function add_ending_slash($path) {
return rtrim($path, "/") . "/";
}

In WordPress geht es noch einfacher

Slash Hinzufügen

<?php trailingslashit( $string ) ?>

Hier wird ein eventuell vorhandener Slash er entfernt und dann neu hinzugefügt. So wird ein doppelter Slash am ende des Pfades verhindert.

Slash entfernen

<?php untrailingslashit( $string ); ?>
Werbung