Search code examples
phppreg-replacepreg-match

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


i want to remove

http://
ftp:// 
https://
ftps://
http://www.
https://www.
ftp://www.
ftps://www.
www.

we want to remove if anyone above with domain url. we want only show url

domain.com

we try preg_replacefor http and https. its work fine for both, but its not work. if found www or ftp

preg_replace('#^https?://#', '', $str)

Solution

  • Might be a better one but this should do it:

    $result = preg_replace('#^(https?://|ftps?://)?(www.)?#', '', $str);
    
    • (http with optional ? s :// OR | ftp with optional ? s ://) with both optional ?
    • www. optional ?