how can i match the last part of a URL?
Moderator: General Moderators
-
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?
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
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
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:
there will be steps there though, you will need to place the stripped tags somewhere, and then reference them as the $condition
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
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
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
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
.....or you can just use basename()
-
fredmcfly2
- Forum Newbie
- Posts: 9
- Joined: Tue Sep 27, 2005 2:41 pm
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
<?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
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Read feyd suggestion..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
-
phoenix121
- Forum Commoner
- Posts: 28
- Joined: Sun Sep 25, 2005 9:09 pm
- Location: New Zealand
-
phoenix121
- Forum Commoner
- Posts: 28
- Joined: Sun Sep 25, 2005 9:09 pm
- Location: New Zealand
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:
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));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']);