Antwort an „Gunnar Bittersmann“ verfassen

@@Gerhard

Eine unglückliche Lösung für das obige Beispiel habe ich mit

$test = explode('/','dir/subdir/irgendwas.html'); $test = explode('.',$test[2]); echo $test[0];

gefunden.

Allerdings funktioniert die nur weil ich weiß, dass vor dem irgendwas zwei directorys stehen. Es könnten aber mehr oder weniger sein.

In JavaScript bekommt man mit dem Index −1 das letzte Element, mit −2 das vorletzte usw.:

const mainCharacters = [
	'James T. Kirk',
	'Spock',
	'Leonard “Bones” McCoy',
];

console.log(mainCharacters.at(-1)); // "Leonard “Bones” McCoy"
console.log(mainCharacters.at(-2)); // "Spock"

In PHP gibt es das nicht?

(Nicht, dass man das in diesem Fall bräuchte, wie @Ryuno-Ki schon sagte.)

🖖 Live long and prosper

Nachtrag: Gibt’s: mit count(). $test[count($test) - 1] sollte das letzte Element liefern.

--
“In my home, the America I love, the America I've written about, that has been a beacon of hope and liberty for 250 years, is currently in the hands of a corrupt, incompetent and treasonous administration. Tonight, we ask all who believe in democracy and the best of our American spirit, to rise with us, raise your voices against authoritarianism, and let freedom reign.”
— Bruce Springsteen, Manchester 2025-05-14
freiwillig, öffentlich sichtbar
freiwillig, öffentlich sichtbar
freiwillig, öffentlich sichtbar

Ihre Identität in einem Cookie zu speichern erlaubt es Ihnen, Ihre Beiträge zu editieren. Außerdem müssen Sie dann bei neuen Beiträgen nicht mehr die Felder Name, E-Mail und Homepage ausfüllen.

abbrechen