note 56817 deleted from function.set-include-path by aidan

From: Date: Sat, 15 Apr 2006 16:31:38 +0000
Subject: note 56817 deleted from function.set-include-path by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-108155@lists.php.net to get a copy of this message
Note Submitter: kencomer at NOSPAM dot kencomer dot com ---- A handy-dandy function to add to the include path while eliminating duplicates (two versions) with a testbed so you can check it on your machine: <head></head> <body> <?php /** * Add a directory to the php include path without adding a duplicate * * <p> * </p> * <p>This is a file from * </p> * @author Ken Comer <kencomer@kencomer.com> * @package * @subpackage * @version * @copyright was &copy; 2005 Ken Comer, released to public domain by author */ /** * short version, no intermediate storage * * @author Ken Comer <kencomer@kencomer.com> */ // does not use get_include_path so as to be compatible with PHP4 if (!defined('SEP')) define('SEP',PATH_SEPARATOR); // PHP Magic Constant function add_include_path($paths,$sep=SEP) { ini_set( 'include_path', implode( $sep ,array_flip(array_flip( // flipping twice removes duplicates array_merge( explode( $sep ,$ini_get('include_path') ) ,explode( $sep ,$paths ) ) )) ) ); } /** * vanilla version with intermediate storage and documentation * * @author Ken Comer <kencomer@kencomer.com> */ // does not use get_include_path so as to be compatible with PHP4 function add_include_path_long($paths,$sep=SEP) { // following lines transform '/path1;path2' // into array( 1=>'/path1', 2=>'/path2' ) $ini_paths = explode($sep,ini_get('include_path')) ; $passed_paths = explode($sep,$paths); // combines array( 1=>'/path1', 2=>'/path2' ) // and array( 1=>'/path2', 2=>'/path3' ) // into array( 1=>'/path1', 2=>'/path2', 3=>'/path2', 4=>'/path3' ) $combined_paths = array_merge($ini_paths,$passed_paths); // flipping once makes the paths into the keys, // duplicate keys are eliminated automatically // array( 1=>'/path1', 2=>'/path2', 3=>'/path2', 4=>'/path3' ) // becomes array('/path1'=>1, '/path2'=>2, '/path3'=>3 ) // flipping again returns the array back // into array( 1=>'/path1', 2=>'/path2', 3=>'/path3' ) $combined_paths = array_flip(array_flip( $combined_paths )); // turn array( 1=>'/path1', 2=>'/path2', 3=>'/path3' ) // into '/path1;/path2;/path3' $ini_paths = implode($sep,$combined_paths); ini_set('include_path',$ini_paths ); } ?> <?php // verification testbed echo "include_path=".get_include_path(); add_include_path_long('./include'); echo "; after adding ./include =".get_include_path()."\n"; ?> </body>

« previous php.notes (#108155) next »