note 27931 modified in function.substr by jmcastagnetto

From: Date: Fri, 27 Dec 2002 04:45:12 +0000
Subject: note 27931 modified in function.substr by jmcastagnetto
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-41333@lists.php.net to get a copy of this message
The code below is an alternative to the basename() function in PHP, and it is here to illustrate the use of the substr() function. /******************************************** Getting the file name from an address like http://www.xpto12345.xyz/abc/def/ghi.php using substr() function ********************************************/ // This procedure only works when the filename // is at the end of the string $fn="abc/def/ghi/jkl.php"; // 'where is the filename' string $a = strlen("$fn"); while ($a>0){ if(substr(substr($fn, $a, strlen($fn)), 0, 1)=="/"){break;} $b=substr($fn, $a, strlen($fn)); $a=$a-1; } print $b; --was-- The code below is an alternative to the basname() function in PHP, and it is here to illustrate the use of the substr() function. /******************************************** Getting the file name from an address like http://www.xpto12345.xyz/abc/def/ghi.php using substr() function ********************************************/ // This procedure only works when the filename // is at the end of the string $fn="abc/def/ghi/jkl.php"; // 'where is the filename' string $a = strlen("$fn"); while ($a>0){ if(substr(substr($fn, $a, strlen($fn)), 0, 1)=="/"){break;} $b=substr($fn, $a, strlen($fn)); $a=$a-1; } print $b; http://www.php.net/manual/en/function.substr.php

« previous php.notes (#41333) next »