Page 1 of 1

Question about URLs

Posted: Wed Jul 02, 2008 4:33 pm
by RafaelLopez
Hello all, I'm new, name's Rafael.

Here's something that must be very simple to accomplish but I can't find the solution anywhere. :banghead: Maybe someone has an idea or can point me to the nearest manual entry.

I'd simply like to store the name of the document's file in a variable. Of course, PHP is to guess the document's name. So let's say I have a script at http://www.thissite.com/boring.php, and I'd like it to just print "boring" by just looking at the URL, stripping it of domain name, path and extension...

Trimming down the string wouldn't be difficult to figure out, but how do I store the document's name in a variable for starters?

Thanks a million...

Re: Question about URLs

Posted: Wed Jul 02, 2008 4:35 pm
by John Cartwright
Are the extensions going to differ? If not, you can simply do

Code: Select all

$filename = basename('http://www.thissite.com/boring.php', '.php');

Re: Question about URLs

Posted: Wed Jul 02, 2008 4:45 pm
by RafaelLopez
Jcart wrote:Are the extensions going to differ? If not, you can simply do

Code: Select all

$filename = basename('http://www.thissite.com/boring.php', '.php');
That's the solution to trim down the file name into just its "boring" name, but only if I already know the filename beforehand. Instead I want PHP to figure out that for me, can that be done?

Let's say I have several scripts in different php files, and some file names carry a code. So the same script should make different scripts do the same different, boring.php should print "boring", foo.php prints "foo" and so on.

Re: Question about URLs

Posted: Wed Jul 02, 2008 4:52 pm
by John Cartwright
Umm.. did you even try my code?

Re: Question about URLs

Posted: Wed Jul 02, 2008 5:04 pm
by RafaelLopez
Jcart wrote:Umm.. did you even try my code?
Jcart, I think you misunderstood, your script will only work on boring.php specifically. I want to make an include that figures out the name of *any* php file.

That line's more likely to look like

Code: Select all

$filename = basename($url, '.php');
What I need is a code to figure out variable $url... there's *got* to be a some simple solution to that!

Re: Question about URLs

Posted: Wed Jul 02, 2008 5:09 pm
by John Cartwright
I understand. I was just giving an example.

However, what I don't understand is what you are having trouble with. Are you asking how to set $url? Where are you getting your list of urls?

Re: Question about URLs

Posted: Wed Jul 02, 2008 5:22 pm
by RafaelLopez
Jcart wrote:I understand. I was just giving an example.

However, what I don't understand is what you are having trouble with. Are you asking how to set $url? Where are you getting your list of urls?
There's no list of URLs, I need a function that sets $url to the current URL, is all.

Re: Question about URLs

Posted: Wed Jul 02, 2008 5:37 pm
by jayshields
Look at the $_SERVER superglobal.

Re: Question about URLs

Posted: Wed Jul 02, 2008 8:03 pm
by John Cartwright
or the __FILE__ constant

Re: Question about URLs

Posted: Thu Jul 03, 2008 2:48 pm
by RafaelLopez
Jcart wrote:or the __FILE__ constant
That looks like it! Thanks!