Gestion des chemins en PHP
March 12th, 2008
Comme j'en avais marre de devoir constament verrifier si les chemins avec lesquels je travaillais finissaient oui ou non par "/", j'ai décidé de faire une classe pour gérer ça de la manière la plus simple possible. Vous pouvez télécharger cette classe aux formats 7zip, Bzip2 ou Zip.
Voici l'exemple d'utilisation que j'ai fait :
PHP:
-
<?php
-
// First of all, we need to include the class
-
require_once "class.path.php";
-
-
// Then we'll create a new path, with all options
-
// In order, the options are : the path string, is the path pointing a
-
// directory, is there some string to put before the path, and what is the
-
// separator ?
-
$path = new Path("/some/path/to/test/", true, "http://", "/");
-
-
// Obviously, I want to output an URL, just look
-
echo "It's looking like an URL: $path\n";
-
-
// Now I create a new path with a completely different delimiter
-
$path2 = new Path("\a\file.php", false, null, "\\");
-
-
// And I can just append it to the previous one
-
$path->a($path2);
-
// We could have done $path->b($path2), but it would have returned a new object
-
// with $path2 appened instead of changing $path
-
-
// And turn this into a SMB share
-
$path->setHead("\\\\");
-
$path->setSeparator("\\");
-
echo "Now it's a SMB share: $path\n";
-
-
// You can also use new basename() and dirname()
-
// Note that dirname() returns a Path and not a string
-
?>
Entry Filed under: Programmation, PHP, Classes
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed