regex to get current page or directory name?

Posted by John Isaacks on Stack Overflow See other posts from Stack Overflow or by John Isaacks
Published on 2010-04-28T21:33:19Z Indexed on 2010/04/28 21:37 UTC
Read the original article Hit count: 403

Filed under:
|

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!!!

© Stack Overflow or respective owner

Related posts about php

Related posts about regex