To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 10-06-2003, 08:09 AM   #1
The Chancer
Unrated...
 
The Chancer's Avatar
 
Join Date: Aug 2002
Location: Flying... always flying (apart from when crashed)
Posts: 1,299
God damn ugly looking line o' code

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...

Code:
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 ??
__________________
The Chancer
----------------
Does This Mean I'm Bored
The Chancer is offline   Reply With Quote
Old 10-06-2003, 01:25 PM   #2
bad76
GuruMeditation #8401.0007
 
bad76's Avatar
 
Join Date: May 2003
Location: Italy
Posts: 1,213
Can i hint you to use strrchr() instead of strchr(strrev(... ?
May be strrchr() is also more efficent, other than readable...
__________________
It's statistically proven that anything can be proven by means of statistics...
bad76 is offline   Reply With Quote
Old 10-07-2003, 07:14 AM   #3
The Chancer
Unrated...
 
The Chancer's Avatar
 
Join Date: Aug 2002
Location: Flying... always flying (apart from when crashed)
Posts: 1,299
OK... this also gives me the parent dir of the working dir, but it also looks damn ugly....

PHP Code:
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.....
__________________
The Chancer
----------------
Does This Mean I'm Bored
The Chancer is offline   Reply With Quote
Old 10-07-2003, 09:34 AM   #4
OhLordy
Country Bumpkin
 
OhLordy's Avatar
 
Join Date: Feb 2003
Location: Surrey / UK
Posts: 511
On *nix machines
PHP Code:
<?php
echo(shell_exec("../; pwd"));
?>
not sure if that's cheating?
Rob
__________________
Bubble
Honest, it is me. I'm a muppet! I've forgoten my passoword and my email address is my work one so I can't get it while I'm at home.
OhLordy is offline   Reply With Quote
Old 10-07-2003, 01:56 PM   #5
PyroX
Ancient Member
 
PyroX's Avatar
 
Join Date: Sep 2002
Location: Iowa
Posts: 420
This code will not work:
Quote:
<?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;
}

__________________
No matter how advanced, artificial intelligence is no match for natural stupidity.

Last edited by PyroX; 10-07-2003 at 02:11 PM.
PyroX is offline   Reply With Quote
Old 10-07-2003, 07:04 PM   #6
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,126
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:
PHP Code:
echo substr($_SERVER['PHP_SELF'],1,
    -(
    
strlen(strrchr($_SERVER['PHP_SELF'], "/"))
    +
    
strlen(strrchr(dirname($_SERVER['PHP_SELF']), "/"))
    )
);
Not to mention using regexps:
PHP Code:
preg_match("!^.(.*?)(/[^/]+){2}$!", $_SERVER['PHP_SELF'], $parent);
echo
$parent[1];
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.

Last edited by Weedpacket; 10-07-2003 at 07:21 PM.
Weedpacket is offline   Reply With Quote
Old 10-08-2003, 06:34 AM   #7
The Chancer
Unrated...
 
The Chancer's Avatar
 
Join Date: Aug 2002
Location: Flying... always flying (apart from when crashed)
Posts: 1,299
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...
__________________
The Chancer
----------------
Does This Mean I'm Bored
The Chancer is offline   Reply With Quote
Old 10-08-2003, 04:39 PM   #8
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,126
Quote:
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.
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 10-09-2003, 07:54 AM   #9
The Chancer
Unrated...
 
The Chancer's Avatar
 
Join Date: Aug 2002
Location: Flying... always flying (apart from when crashed)
Posts: 1,299
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.
__________________
The Chancer
----------------
Does This Mean I'm Bored
The Chancer is offline   Reply With Quote
Old 10-09-2003, 02:01 PM   #10
tekky
the stand in Telepath
 
tekky's Avatar
 
Join Date: Jul 2003
Location: College Station, Texas
Posts: 2,293
I think these all cross over to PHP also....

http://www.mysql.com/doc/en/Regexp.html
__________________
-Karl
Problem solved? Mark your thread resolved!
See how many times my link has been used to mark a thread resolved here
(fixed to work with updated VBulletin... this works again!)
tekky is offline   Reply With Quote
Old 10-10-2003, 08:05 AM   #11
The Chancer
Unrated...
 
The Chancer's Avatar
 
Join Date: Aug 2002
Location: Flying... always flying (apart from when crashed)
Posts: 1,299
I have now found this which could well be of help....

Seems to cover just about everything..
__________________
The Chancer
----------------
Does This Mean I'm Bored
The Chancer is offline   Reply With Quote
Old 10-11-2003, 11:13 PM   #12
buducom
Member
 
Join Date: Jul 2003
Posts: 73
dan

explode the path string and use the last elements to refer

dan
buducom is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 09:40 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.