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

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

    I'd suggest using the tools PHP gave you, have a look at parse_url.

    
    

    The above example will output:

    Array
    (
        [scheme] => http
        [host] => hostname
        [user] => username
        [pass] => password
        [path] => /path
        [query] => arg=value
        [fragment] => anchor
    )
    /path
    

    It sounds like you're after at least host + path (add others as needed, e.g. query):

    $parsed = parse_url('http://www.domain.com/topic/questions/');
    
    echo $parsed['host'], $parsed['path'];
    
        > www.domain.com/topic/questions/
    

    Cheers

提交回复
热议问题