Click to See Complete Forum and Search --> : God damn ugly looking line o' code
The Chancer
10-06-2003, 08:09 AM
OK - this works - but looks damn ugly....
Scenario....
In a page at
localhost/ccs/php/menu.php
Dynamic link needed to
localhost/ccs/admin/index.php
This is to aid portability so when the working directory has changed (eg CCS) the links will still be active...
http://".$_SERVER['HTTP_HOST'].strrev( strchr(strrev(str_replace($_SERVER['DOCUMENT_ROOT'], "", dirname($_SERVER['PHP_SELF']))),'/'))."admin/main.html
Is there a nicer way of doing this ??
bad76
10-06-2003, 01:25 PM
Can i hint you to use strrchr() instead of strchr(strrev(... ?
May be strrchr() (http://www.php.net/strrchr) is also more efficent, other than readable...
The Chancer
10-07-2003, 07:14 AM
OK... this also gives me the parent dir of the working dir, but it also looks damn ugly....
echo substr($_SERVER['PHP_SELF'],1,-(
(strlen(substr(strrchr($_SERVER['PHP_SELF'], "/"), 1))+1)+
(strlen(substr(strrchr(dirname($_SERVER['PHP_SELF']), "/"), 1))+1)
));
There may be many different ways of doing this - but some I am sure will look a hell of a lot better than this.....
OhLordy
10-07-2003, 09:34 AM
On *nix machines
<?php
echo(shell_exec("../; pwd"));
?>
not sure if that's cheating?
Rob
PyroX
10-07-2003, 01:56 PM
This code will not work:
<?php
echo(shell_exec("../; pwd"));
?>
Instead try this:
echo getpath('../');
function getpath($path){
$rpath=trim(`pwd`);
chdir($path);
$path = `pwd`;
chdir($rpath);
return $path;
}
Also here is an example and function to just get the path one notch up from the current path:
echo parentpath();
function parentpath(){
$rpath=trim(`pwd`);
chdir('../');
$path = `pwd`;
$path = split('/',$path);
$path = $path[count($path)-1];
chdir($rpath);
return $path;
}
Weedpacket
10-07-2003, 07:04 PM
I'm still playing with the string-handling method. moving the negation in, and noting that strlen(substr(foo,1)) = strlen(foo)-1 I get so far:
echo substr($_SERVER['PHP_SELF'],1,
-(
strlen(strrchr($_SERVER['PHP_SELF'], "/"))
+
strlen(strrchr(dirname($_SERVER['PHP_SELF']), "/"))
)
);
Not to mention using regexps:
preg_match("!^.(.*?)(/[^/]+){2}$!", $_SERVER['PHP_SELF'], $parent);
echo $parent[1];
The Chancer
10-08-2003, 06:34 AM
Ahh, weedpackets first code example seems a much simpler way, and considering that strlen(substr(foo,1)) = strlen(foo)-1 (which I hadn't realised) is a short hop from my example.
The regexp snippet however does intrigue me, and would be my choice - if only I understood it fully. I have seen somewhere, an explanation of them, but can't find it again... anyone point me in the right direction ?
Pyrox - while the function is valid - I was looking for more of a one liner - as code space is a consideration...:D
Weedpacket
10-08-2003, 04:39 PM
Originally posted by The Chancer
The regexp snippet however does intrigue me, and would be my choice - if only I understood it fully. I have seen somewhere, an explanation of them, but can't find it again... anyone point me in the right direction ?
I've actually been considering writing a PHPBuilder article on the subject myself for some time.
Something I've realised a little later about that regexp is how easily it could be modified to get the grandparent directory instead of the parent. The bit to check out is the {2}. If that were replaced by {0} you'd get everything, including the file itself. If it's {1} you get the path up to and including the directory in which the file resides. Use {2} and you get the path to the parent directory. {3} gives you the grandparent's path, and so on up the whakapapa.
The Chancer
10-09-2003, 07:54 AM
All I can say to that is then please do....
Even if it was a listing to show what the ^ ! ? {} mean within it, as well as any ordering that needs to take place - it would be somewhere to start.
tekky
10-09-2003, 02:01 PM
I think these all cross over to PHP also....
http://www.mysql.com/doc/en/Regexp.html
The Chancer
10-10-2003, 08:05 AM
I have now found this (http://etext.lib.virginia.edu/helpsheets/regex.html) which could well be of help....
Seems to cover just about everything..
buducom
10-11-2003, 11:13 PM
explode the path string and use the last elements to refer
dan
PHP Builder
Copyright WebMediaBrands Inc. All Rights Reserved.