tags:

views:

86

answers:

8

I am trying to get the page or last directory name from a url

for example if the url is: http://www.example.com/dir/ i want it to return dir or if the passed url is http://www.example.com/page.php I want it to return page Notice I do not want the trailing slash or file extension.

I tried this:

$regex = "/.*\.(com|gov|org|net|mil|edu)/([a-z_\-]+).*/i";

$name = strtolower(preg_replace($regex,"$2",$url));

I ran this regex in PHP and it returned nothing. (however I tested the same regex in ActionScript and it worked!)

So what am I doing wrong here, how do I get what I want?

Thanks!!!

+3  A: 

Don't use / as the regex delimiter if it also contains slashes. Try this:

$regex = "#^.*\.(com|gov|org|net|mil|edu)/([a-z_\-]+).*$#i";
Mark Byers
+1 for showing me another way to wrap regex :)
John Isaacks
+3  A: 

You may try tho escape the "/" in the middle. That simply closes your regex. So this may work:

$regex = "/.*\.(com|gov|org|net|mil|edu)\/([a-z_\-]+).*/i";

You may also make the regex somewhat more general, but that's another problem.

petersohn
A: 

You can use this

array_pop(explode('/', $url));

Then apply a simple regex to remove any file extension

Manos Dilaverakis
A: 
Dereleased
A: 
Trey
A: 

As much as I personally love using regular expressions, more 'crude' (for want of a better word) string functions might be a good alternative for you. The snippet below uses sscanf to parse the path part of the URL for the first bunch of letters.

$url  = "http://www.example.com/page.php";
$path = parse_url($url, PHP_URL_PATH);
sscanf($path, '/%[a-z]', $part);
// $part = "page";
salathe
This has a serious problem that anything other than letters a-z in the filename will be matched. What about capitals, numbers, symbols, among other things?
erisco
What about them? If anything, according to the original question, the only extra things needed in the character class would be underscore and hyphen. If the OP needs "other things" I'm sure he can ask or figure out himself.
salathe
A: 

This expression:

(?<=^[^:]+://[^.]+(?:\.[^.]+)*/)[^/]*(?=\.[^.]+$|/$)

Gives the following results:

http://www.example.com/dir/            dir
http://www.example.com/foo/dir/        dir
http://www.example.com/page.php        page
http://www.example.com/foo/page.php    page

Apologies in advance if this is not valid PHP regex - I tested it using RegexBuddy.

Damian Powell
A: 

Save yourself the regular expression and make PHP's other functions feel more loved.

$url  = "http://www.example.com/page.php";
$filename = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_FILENAME);

Warning: for PHP 5.2 and up.

erisco
This has a serious problem that anything other than base-level file or folder name will be matched. What about `.../page/foo.php` among other things?
salathe
The requirement is to "get the page or last directory name." This means we do not want the entire path, merely "foo" from your example. Be this not the requirement then the question was poorly asked.
erisco