Question about URLs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RafaelLopez
Forum Newbie
Posts: 6
Joined: Wed Jul 02, 2008 4:24 pm

Question about URLs

Post 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...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Question about URLs

Post 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');
RafaelLopez
Forum Newbie
Posts: 6
Joined: Wed Jul 02, 2008 4:24 pm

Re: Question about URLs

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Question about URLs

Post by John Cartwright »

Umm.. did you even try my code?
RafaelLopez
Forum Newbie
Posts: 6
Joined: Wed Jul 02, 2008 4:24 pm

Re: Question about URLs

Post 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!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Question about URLs

Post 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?
RafaelLopez
Forum Newbie
Posts: 6
Joined: Wed Jul 02, 2008 4:24 pm

Re: Question about URLs

Post 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.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Question about URLs

Post by jayshields »

Look at the $_SERVER superglobal.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Question about URLs

Post by John Cartwright »

or the __FILE__ constant
RafaelLopez
Forum Newbie
Posts: 6
Joined: Wed Jul 02, 2008 4:24 pm

Re: Question about URLs

Post by RafaelLopez »

Jcart wrote:or the __FILE__ constant
That looks like it! Thanks!
Post Reply