开发者

regex to get current page or directory name?

开发者 https://www.devze.com 2022-12-28 07:27 出处:网络
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

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 = "/开发者_C百科.*\.(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!!!


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

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


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.


You can use this

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

Then apply a simple regex to remove any file extension


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.


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));


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";


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消