Friday, April 20, 2012

how to Strip filename from a URL with php

Using PHP, you might need to strip the path from a URL, leaving behind just the filename at the end of the URL. You can achieve this with a regular expression pattern of course, but I have a much simpler solution. See the example code below.

$url = "http://www.domain.com/path/to/file/filename.php";
$filename = basename($url);
echo $filename; //filename.php
And that’s all there is to it. I hope that you found this useful.

1 comment:

  1. Thanks, that's pretty helpful.
    It would be better to use a custom style for code source or shell commands ..etc

    for example : http://alexgorbatchev.com/SyntaxHighlighter/

    keep up the good work and keep SHARING

    ReplyDelete