how can i match the last part of a URL?

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
phoenix121
Forum Commoner
Posts: 28
Joined: Sun Sep 25, 2005 9:09 pm
Location: New Zealand

how can i match the last part of a URL?

Post by phoenix121 »

for example, in the URL:

http://www.abc.com/xxx/qwerty.html

how do i get just the "qwerty.html" bit (and only get it if it's there)?

thanks in advance
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post by $var »

you'll have to be a little more descriptive.
get it and put it where, get it if it's there and do what with it?

sounds like you need either:
trim() - http://ca.php.net/trim

or

strip_tags() - http://ca.php.net/manual/en/function.strip-tags.php

this might be what you are looking for to tell if it is there:

Code: Select all

<? $condition = $regresults["Reg_URL"];
							if (empty($with)) {
							echo "";
							}
							else {
							echo "<a href='$with' target='_blank'>"; 
							echo "<img border='0' width='20' height='20' src='images/icons/download_b.jpg'>"; 
							echo "</a>"; 
							}
							?>

there will be steps there though, you will need to place the stripped tags somewhere, and then reference them as the $condition
fredmcfly2
Forum Newbie
Posts: 9
Joined: Tue Sep 27, 2005 2:41 pm

Post by fredmcfly2 »

You could write a parser. You would ignore everything up to the third "/" then get all the letters after it. If you run into a "/" then clear the data and start again, once you hit a period "." you know you have the file name then keep getting letters until you hit a null to get the ending i.e. html, htm, php.

Here are the steps:
1. ignore all the letters until you hit the third "/" i.e. don't store them anywhere
2. Start storing letters, if you hit another "/" you know it isn't a file name and clear out the letters
You have to start storing letters here because the url might be http://www.abc.com/qqwert.html
3. if you hit a period you know you have a filename and keep going until you get the suffix
phoenix121
Forum Commoner
Posts: 28
Joined: Sun Sep 25, 2005 9:09 pm
Location: New Zealand

Post by phoenix121 »

fredmcfly2, i need help to make the parser. i know what you mean, but if i make it with my limited knowledge, it will probably be very cumbersome. please help!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

.....or you can just use basename()
fredmcfly2
Forum Newbie
Posts: 9
Joined: Tue Sep 27, 2005 2:41 pm

Post by fredmcfly2 »

Use

<?php
echo $_SERVER['SCRIPT_NAME']
?>

to get the url. If your url it http://www.abc.com/stuff/stuff.php then $_SERVER['SCRIPT_NAME'] will return /stuff/stuff.php

I just realized a much easier way to do this... use explode()

Your code will look something like this:

$URLstring = $_SERVER['SCRIPT_NAME'];

array = explode('/', $URLstring); //this parses for you every time it sees a '/'
$filename = array[1]; //$filename will hold stuff.php
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

fredmcfly2 wrote:Use

<?php
echo $_SERVER['SCRIPT_NAME']
?>

to get the url. If your url it http://www.abc.com/stuff/stuff.php then $_SERVER['SCRIPT_NAME'] will return /stuff/stuff.php

I just realized a much easier way to do this... use explode()

Your code will look something like this:

$URLstring = $_SERVER['SCRIPT_NAME'];

array = explode('/', $URLstring); //this parses for you every time it sees a '/'
$filename = array[1]; //$filename will hold stuff.php
Read feyd suggestion.. :P
phoenix121
Forum Commoner
Posts: 28
Joined: Sun Sep 25, 2005 9:09 pm
Location: New Zealand

Post by phoenix121 »

lol, i didn't see feyd's suggestion! :?
phoenix121
Forum Commoner
Posts: 28
Joined: Sun Sep 25, 2005 9:09 pm
Location: New Zealand

Post by phoenix121 »

i actually want the front bit, so dirname() is more useful. but dirname() always cuts off everything after the last slash. I want to keep the last bit if its a directory, but cut it off if it's a file... is this possible?

i thought of a way around it, by using basedir() and preg_match() to see if the last bit has any valid suffixes. If it does have any valid suffixes, then cut it off, but if it doesn't, leave it on. But that would be a very bad way to do it.

i'd be using code like:

Code: Select all

$searchstring = '\.(html|htm|php|asp|cfm|phtml|jsp)';   //etc.
$results = preg_match($searchstring, basedir($address));
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it'd require fiddling with is_dir()
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

dirname() is a pain, made worse by working different on the different OSs. If you want the path from document root but not including the file name try:

Code: Select all

$path_from_root = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
Post Reply