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

后端 未结 10 2070
不思量自难忘°
不思量自难忘° 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:55

    Create an array:

    $remove = array("http://","https://");

    and replace with empty string:

    str_replace($remove,"",$url);

    it would look something like this:

    function removeProtocol($url){
        $remove = array("http://","https://");
        return str_replace($remove,"",$url);
    }
    

    Str_replace will return a string if your haystack (input) is a string and you replace your needle(s) in the array with a string. It's nice so you can avoid all the extra looping.

    Happy Coding!

提交回复
热议问题