RE: [PHP] Removing script name

From: Date: Mon, 16 Oct 2000 20:33:28 +0000
Subject: RE: [PHP] Removing script name
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-20454@lists.php.net to get a copy of this message
Hi Todd, sorry, I made a mistake (wrong way). NOT: > > If I have C:\MyDir\MyFile.txt, how can I remove the "MyFile.Txt" so that > > I have "C:\MyDir\" remaining. I have never mastered the pattern > > matching in Perl!! > > you can do this in php in three diffrent ways: > > <? > // This is the most efficient > $path = "C:\\MyDir\\MyFile.txt"; > $file = substr($file,strrpos($path,"\\")+1,strlen($path)); > print $file."<br>\n"; > > // Faster than ereg_*-function > preg_match("/.*\\\\([^\\\\]+)$/",$path,$file); > print $file[1]"<br>\n"; > > // Slowest > $file = ereg_replace(".*\\\\([^\\\\]+)$","\\1",$path); > print $file"<br>\n"; > ?> This is right: <? $path = "C:\\MyDir\\MyFile.txt"; $file = substr($path,0,strrpos($path,"\\")+1); print $file."<br>\n"; preg_match("/(.*\\\\)[^\\\\]+$/",$path,$file); print $file[1]."<br>\n"; $file = ereg_replace("(.*\\\\)[^\\\\]+$","\\1",$path); print $file."<br>\n"; ?> Cheers, Matthias aka Matthew

« previous php.general (#20454) next »