2

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
  • "however I tested the same regex in ActionScript and it worked!" Did you manually set $url or did you pull it from somewhere else, like one of the $_SERVER values?
    – Powerlord
    Apr 28, 2010 at 21:36
  • Do you want to return the whole address after the domain part of the URL? Apr 28, 2010 at 21:39
  • @OMG Unicorns I am manually setting the URL, not getting it from $_SEVER
    – JD Isaacks
    Apr 29, 2010 at 12:52

8 Answers 8

6

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

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

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.

0

You can use this

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

Then apply a simple regex to remove any file extension

0

Assuming you want to match the entire address after the domain portion:

$regex = "%://[^/]+/([^?#]+)%i";

The above assumes a URL of the format extension://domainpart/everythingelse.

0

Then again, it seems that the problem here isn't that your RegEx isn't powerful enough, just mistyped (closing delimiter in the middle of the string). I'll leave this up for posterity, but I strongly recommend you check out PHP's parse_url() method.

This should adequately deliver:

substr($s = basename($_SERVER['REQUEST_URI']), 0, strrpos($s,'.') ?: strlen($s))

But this is better:

preg_replace('/[#\.\?].*/','',basename($path));

Although, your example is short, so I cannot tell if you want to preserve the entire path or just the last element of it. The preceding example will only preserve the last piece, but this should save the whole path while being generic enough to work with just about anything that can be thrown at you:

preg_replace('~(?:/$|[#\.\?].*)~','',substr(parse_url($path, PHP_URL_PATH),1));
0

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";
2
  • 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
    Apr 29, 2010 at 8:32
  • 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
    Apr 29, 2010 at 12:13
0

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.

0

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.

2
  • 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
    Apr 29, 2010 at 12:16
  • 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
    Apr 29, 2010 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.