使用正则表达式修复PHP中常见的URL错误[关闭]

Im not good at regular expressions so I need some help. How to fix following URL mistakes using RegEx?

  • https:/
  • https/
  • https//
  • http//
  • http:/

you can use the following code

$re = '/^(https?)(\:?\/?\/?)/';
$str = 'https:/
https/
https//
http//
http:/';
$subst = '\\1://';

$result = preg_replace($re, $subst, $str);

echo "The result of the substitution is ".$result;

the regex

/^(https?)(:?/?/?)/

matches http/https in the first group, every other possibility is in the 2nd group
just replace the 2nd group with the correct value every time
see demo