user3468831
user3468831

Reputation: 37

Get the filename part from a given URL

I have a URL like below:

http://abc.def.com/gh-tqa/name-of-the-file/ZW607CCF.html

Now I want to get the string of ZW607CCF without .html.

How can I achieve this using PHP?

Upvotes: 0

Views: 63

Answers (3)

ponciste
ponciste

Reputation: 2229

this is how you do:

$temp = explode('.', $_SERVER['PHP_SELF']);

$string = $temp[0];

with PHP 5.4+ you can also do:

$string = explode('.', $_SERVER['PHP_SELF'])[0];

Upvotes: 0

Amal Murali
Amal Murali

Reputation: 76646

You don't need a regex here. Just use pathinfo():

$url = 'http://abc.def.com/gh-tqa/name-of-the-file/ZW607CCF.html';
$filename = pathinfo($url, PATHINFO_FILENAME);

Upvotes: 10

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

There are lot of ways in PHP, try preg_replace(), for example:

$url = 'http://abc.def.com/gh-tqa/name-of-the-file/ZW607CCF.html';
$value = preg_replace("|.*/([^.]+)\.html|", "$1", $url);

Upvotes: 0

Related Questions