php - Remove last slash in URL, only if no directory is present -
i trying remove last / url, if there no directory present. there way check if(3 slashes && not https) remove slash? or there better way accomplish trying do?
what have far
$url = preg_replace(array('{http://}', '{/$}'), '', $project->url); current outputs:
http://www.example.org/ => www.example.org https://www.example.org/ => https://www.example.org http://www.example.org/dir/ => www.example.org/dir https://www.example.org/dir/ => https://www.example.org/dir http://www.example.org/dir/dir/ => www.example.org/dir/dir https://www.example.org/dir/dir/ => https://www.example.org/dir/dir what want get
http://www.example.org/ => www.example.org https://www.example.org/ => https://www.example.org http://www.example.org/dir/ => www.example.org/dir/ https://www.example.org/dir/ => https://www.example.org/dir/ http://www.example.org/dir/dir/ => www.example.org/dir/dir/ https://www.example.org/dir/dir/ => https://www.example.org/dir/dir/
you can try this:
$url = preg_replace('~^(?:(https://)|http://)?+([^/]++)(?:(/[^\s"']++)|/)?+~', '$1$2$3', $url); or more simple (if $url contains url)
$url = preg_replace('~^(?:(https://)|http://)?+([^/]++)(?:(/.++)|/)?+~', '$1$2$3', $url); note these patterns:
www.example.org/ give www.example.org
and
http://www.example.org give www.example.org
second pattern details
~ # pattern delimiter ^ # anchor begining of string (?:(https://)|http://)?+ # optional "http(s)://" , "https://" # captured in group $1 (not "http://") ([^/]++) # capturing group $2: characters except "/" (?:(/.++)|/)?+ # slash followed characters (capturing group $3) # or slash (not captured), # part optional "?+" ~ # pattern delimiter
Comments
Post a Comment