bensiu
bensiu

Reputation: 25584

extract script name from URL

I looking for regexp help how to extract just script name from url:

I have:

"http://www.example.com/index234.html"

and looking to receive "index234" ?

Upvotes: 1

Views: 1245

Answers (3)

Toto
Toto

Reputation: 91488

Use parse_url

$url = 'http://www.example.com/index234.html';
$parts = parse_url($url);
preg_match('~([^/]+)\..+$~', $parts['path'], $m);
print_r($m);

output:

Array
(
    [0] => index234.html
    [1] => index234
)

Upvotes: 6

fardjad
fardjad

Reputation: 20404

use this regex:

/^http:\/\/(www\.)?example.com\/(?<scriptName>.*)\.html$/

Edit:

this regex would work for different urls and paths

/^http:\/\/(www\.)?(.*\/)*(?<scriptName>.*)\..*$/

Upvotes: 3

LeleDumbo
LeleDumbo

Reputation: 9340

try: http://([\w\-\.]+/)+([\w\-\.]+)\.html the script name is in 2nd capture ($2 or \2). you can adjust the protocol and file extension as required, note that I don't really know what characters are actually allowed for url name, so in this case I assume letters, numbers, hyphens and dots only.

Upvotes: 0

Related Questions