I'd like to incorporate this into a script:
GET chosen page's $ENV{'DOCUMENT_URI'} and $ENV{'DOCUMENT_NAME'}, translate "/" to "_", and use that value for another function.
Chosen page would have an exec cgi call on the page to this script, similar to a counter.cgi
So, we take a DOCUMENT_URI/ DOCUMENT_NAME of:
foo/subfoo/index.html
and change it to:
foo_subfoo_index.html
$NEWVARPATH = "foo_subfoo_";
$NEWVARHTML = "index.html";
This seems so simple, until I hacked away at it for four or five hours. I'm either making it harder than it needs to be, or ignoring necessary regexes. Either way, I'm sunk.
Any recommendations?
(home) /index.html
foo/subfoo/index.html
foo/subfoo/subofsubfoo/other.html
I want to be sure I can split this all up so the paths and document names can be separated.
So far, I have trouble because anything in the home directory just shows as:
/index.html
and gives me no path. Even:
_index.html
would help.
<?
$str = "foo/subfoo/index.html";
// get the position of the last slash:
$pos = strrpos($str, "/");
// increment the variable to go one character beyond the slash:
$pos2 = ++$pos;
// pick off the file name and file extension:
$file = substr($str, $pos2);
// grab the path (from zero to the position of the last slash):
$path = substr($str, 0, $pos);
// change the slashes to underscores:
$path = ereg_replace("/", "_", $path);
echo ("$path<br>\n"); // returns foo_subfoo_
echo ("$file"); // returns index.html
?>
Perhaps there's an easier way, but this works.
But, I think the add / then strip routine is what it will take to make this work. that's where my problems have been.
#!/usr/local/bin/perl
#######Split the various parts of the request page
@new = split(/\//, $ENV{REQUEST_URI});
#######Get the original page name
$NEWVARHTML = $new[-1];
#######Remove the first and last elements leaving only the directories requested
pop @new;
shift @new;
#######Take the directories requested and add the "_" to the end
foreach $new (@new)
{$PATH{$new} = "$new\_";}
########Get only the values for the new path
@NEWVARPATH = values(%PATH);
print "Content-type: text/html\n\n";
print "<br />NEWVARHTML = $NEWVARHTML";
print "<br />NEWVARPATH =@NEWVARPATH";
exit(0);
I tested it with no directories, one directory, and two directories. It worked fine for all of them so it should work beyond that too.
Ohmygosh! You mean you read the original post and decided to start hacking for the heck of it... or you're weird like me and decided you really needed a script to do this? It's so much nicer when you know you aren't alone :)
I'm debugging something else at the moment ,<473 lines down with about 300 to go>, but I'm eager to see what flies later and let you know. Thanks so much for sharing.
Well, it works like a champ. Now I have my paths and pagename's split up I can automatically call all kinds of files based on the document folder and/or filename. Works slick as can be. I had it working when I was blindly hacking away at it - except for the home (base) folder wasn't working. Nothing would show up unless it was a doc in a subfolder. This fixed it.
Thanks a bunch.