note 13886 added to function.dirname.php

From: Date: Sun, 08 Jul 2001 16:00:12 +0000
Subject: note 13886 added to function.dirname.php
Groups: php.notes 
Request: Send a blank email to php-notes+get-15093@lists.php.net to get a copy of this message
For a website I have created, I needed to use the directoryname to define the variable $language. However, dirname gives a total path (i.e. "/~s751294/att/bla/en"), and not just the lowest subdir (i.e. "en"). The script below does! <? // 1. find filename $language = $PHP_SELF; // 2. find dirname $language = dirname($language); // 3. dirname might exist of more than 1 subdirs // if ONE "/" is found: this is the last subdir // if MORE THAN ONE "/" are found: not the last subdir $subdir = 1; while ($subdir) { if (substr_count($language,"/")>1) { // not at the bottom yet... $language = dirname($language); } else { // we are at the bottom! $language = ereg_replace("/","",$language); $subdir = 0; } } echo "this is the lowest directory: $language"; ?> http://www.php.net/manual/en/function.dirname.php

« previous php.notes (#15093) next »