How do I remove http, https and slash from user input in php

后端 未结 10 2071
不思量自难忘°
不思量自难忘° 2020-11-29 04:30

Example user input

http://domain.com/
http://domain.com/topic/
http://domain.com/topic/cars/
http://www.domain.com/topic/questions/

I want

10条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 04:58

    Found this http://refactormycode.com/codes/598-remove-http-from-url-string

    function remove_http($url = '')
    {
        if ($url == 'http://' OR $url == 'https://')
        {
            return $url;
        }
        $matches = substr($url, 0, 7);
        if ($matches=='http://') 
        {
            $url = substr($url, 7);     
        }
        else
        {
            $matches = substr($url, 0, 8);
            if ($matches=='https://') 
            $url = substr($url, 8);
        }
        return $url;
    }
    

提交回复
热议问题