Page 1 of 1

how can i match the last part of a URL?

Posted: Tue Sep 27, 2005 3:19 pm
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

Posted: Tue Sep 27, 2005 3:33 pm
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

Posted: Tue Sep 27, 2005 3:38 pm
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

Posted: Tue Sep 27, 2005 4:10 pm
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!

Posted: Tue Sep 27, 2005 4:25 pm
by feyd
.....or you can just use basename()

Posted: Tue Sep 27, 2005 4:27 pm
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

Posted: Tue Sep 27, 2005 5:03 pm
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

Posted: Tue Sep 27, 2005 5:42 pm
by phoenix121
lol, i didn't see feyd's suggestion! :?

Posted: Tue Sep 27, 2005 5:51 pm
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));

Posted: Tue Sep 27, 2005 6:18 pm
by feyd
it'd require fiddling with is_dir()

Posted: Tue Sep 27, 2005 7:13 pm
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']);