Jump to content

str_replace() great - but... need more


fedoracore

Recommended Posts

hi. i'm looking for something with which i can retrieve the name of my current document and essentially echo it out (or enter it into another string, for further manipulation).this might sound crazy, or just plain stupid-- but what i'm trying to do is use _SERVER['PHP_SELF'] and then to use str_replace() to cut off not only the .php which is part of the name of the file (because i want instead of document_name.php to be simply document_name )of course, depending upon which server your php parser is running, you mght get a different output-- so i need for it to be as versatile as possible. for now, i'll just talk about the apache server on my dev machine._SERVER['php_self'] will actually return /username/webroot/document_name.php -- so (on Linux), i get the ENTIRE path to the doc. (and _SERVER['DOCUMENT_ROOT'] goes all the way back to /var/www/html -- the very root of the Apache server-- not much help here really), and then we have a couple other similar output... but in any case, i'm going to need to strip the information all the way on the "left side" until i hit that final "forward slash".so, i'm thinking that maybe some combination of string functions, and maybe even a regex bit could definitely give the result of the document name, sans .php and sans the leading directory info. (in theory assuming there's no non-apache sever complications of course)is this something that anyone out there might have done before? do you have any suggestions on what i might need to do? i think if i could use 'PHP_SELF' , i could do it if only i can get rid of that info to the left-- and that's where i'm lost for knowledge of advanced string function scripting.i look forward to your reply. thanks!

Link to comment
Share on other sites

$document = ereg("(.*?)/?(.*)\.(.*?)", "$2", $_SERVER['PHP_SELF']);

I hope that is right, because I haven't tested it. Should work though.. Try it out to see if it works, if not tell whats happening.

Link to comment
Share on other sites

Gentlemen and Ladies,I extend profuse thanks to all of the generous replies to my post-- it is unfortunate that I must concomitantly express my apologies for my own delayed response to you. Due to an ISP "buy out", a proper provisioning of my router was finally competed today-- resulting in nearly four days of down-time.it will take me some time to read through what all is here, and see what i come up with. i know this particular project moved forward (while i was offline) to a completed state w/out the function i was trying to implement here (whatever that was-- ha!... naw, i'm pretty sure i recall what i wanted...). hopefully, i'll be able to go back in there and plug-in some code and get it working as i desired, if i feel it's still necessary.again-- thank you all.EDIT:(because i see some where asking...)basically, it was like this:use of _SERVER["SCRIPT_FILENAME"] yeilds (on my Linux, Fedora system running Apache 2.2):/home/username/public_html/phpinfo.php (as the output is indeed coming from the good ol' phpinfo script which we all know and love)other similar "built-in" variables provide similar info-- but NONE will give me simply "phpinfo.php", or "some-other-file.php", or "whatever.php" -- because they will all have /some/kind/of/dir/info/before-my-file.php (you see, working sometimes in ColdFusion will spoil the mind.... into thinking everything is always handed over so easily. nyuk, nyuk... blech!)the idea is to get rid of those "leading" characters, so -- as i see Jhecht has remarked, yes-- i do want that-- at least. i believe i also wanted to remove the right side, so i was left w/ instead of file.php , simply: fileobviously, the dot-php part will be easy if i can get the left side to come out right each time. i saw a regex bit up there-- it was my figuring that a proper regex would be precisely the thing...i'ven't tried it yet...EDIT: (wow-- this is an unusal board behaviour! session cookies stuff i guess-- keeps me in the same post? a first i've seen! )so, it looks like jesh is on top of his game here...pathinfo() seems to do what i want-- however, i know last i checked-- my hosting provider hadn't made the switch to v.5 yet... so, easy come, easy go...:)grrrr.... now i don't like this window-- i just hit the wrong button of the 50 available choices below--- and lost a lot of research supported text. yuck.point was that-- cool-- PATHINFO_FILENAME is cool

Link to comment
Share on other sites

err, that would make something like document.php into documentdocument. So.... err
Try it, it works. And without the regex overhead.It takes basename($_SERVER['PHP_SELF']), which gives you something like 'document.php', and replaces '.php' with '', an empty string, leaving you with 'document'.
Link to comment
Share on other sites

Try it, it works. And without the regex overhead.It takes basename($_SERVER['PHP_SELF']), which gives you something like 'document.php', and replaces '.php' with '', an empty string, leaving you with 'document'.
by george, i believe DeathRay2K has got it!! rock n roll man! it doesn't get much easier... but, do we suffer any potential SERVER inconsistencies, or PHP version (4 and newer)? both basename() and pathinfo() are great stuff to know.... how have i gone about missing these gadgets? too much fuss over the same old same old, i suppose... after all-- it's most of what you see in a lot of code which ends up in forums like this-- maybe people stick w/ talking about the basic food groups for fear of having to actually think, vs regurgitating. i must say, i'm impressed w/ the w3schools forum-- i've used the site for years for html css etc, but never thought of it really as a php resource. i'm definitely going to come back here again. y'all seem nice too. (sorry don't mean to sound like a sap-- but it's truly not often that one finds a decent place to talk shop. believe me, i've done one-off's in several forums which didn't get a quarter of the activity that i saw here. it's important to have such a place-- whether it's facilitated by so-called guru's or not-- just the sense of not being 'alone' in one's struggle... blah, blah-- i don't always talk this much-- just trying to say thanks and i'm glad i joined. peace-out, yo!peace1a(80).jpg:)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...