Tutorial :Remove URL part and file extension with strstr


I tried function: strstr, but it has one problem. Suppose, the URL looks like:


With strstr, I am able to remove anything before '/', but I want just:


i.e., the actual name of the file without extension.


If it always ends in .php you can do:

basename('http://www.example.com/index.php', '.php')  

If it can end with other extensions, you can do:

if (preg_match('#([^/]+)\.\w+$#', 'http://www.example.com/index.php', $matches))      $basename = $matches[1];  


I would highly suggest using the PHP parse_url() function:

$address = 'http://www.example.com/index.php';  $url = parse_url($address);  echo $url['host'];  

There is no point reinventing the wheel.


If the file type can change and you are sure there are no other . in the file name e.g. index.2.php then you can use

$filename = basename('http://www.example.com/index.php');  $filename = substr($filename, 0, strpos($filename, '.'));  


+1 cletus for the right tool for the right job, a proper URL parser. The regex hacks will fail for various query string stuff.

However it's the last path part being sought here not the host. So:

$url = parse_url($address);  $filename= array_pop(explode('/', $url['path']));  $filestem= explode('.', $filename)[0];  


its very simple , no need for much code.

just do it manually and it will be automatically readed

just write your url like that


and it will show your files as there is .php in the end.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »