html - How to remove filename from a url in php -
this question has answer here:
- php how remove last part of path 5 answers
i need write php function remove file name inside url. [i don't want remove last part of url. because url's http://www.examples.com/sample "sample" cannot file. want remove file name.
in. -> http://www.inforge.in/ongoing-works/main_project/index.html out -> http://www.inforge.in/ongoing-works/main_project/ in. -> http://www.inforge.in/ongoing-works/main_project/ out -> http://www.inforge.in/ongoing-works/main_project/ in. -> http://www.inforge.in/ongoing-works/main_project out -> http://www.inforge.in/ongoing-works/main_project/
i think solution check whether '.' [dot] present after last '/'. if so, remove after last '/'. didn't logic write in php
i can't check if there exists '.html', because file name can .php, .jpg, .jsp or that
function stripfile($in){ $pieces = explode("/", $in); // split url / if(count($pieces) < 4) return $in . "/"; if(strpos(end($pieces), ".") !== false){ // got filename @ end array_pop($pieces); // remove filename }elseif(end($pieces) !== ""){ // ends name without extension, i.e. directory $pieces[] = ""; // when $pieces imploded, "/" , "" appended } // else, ends slash return implode("/", $pieces); }
Comments
Post a Comment