Im not good at regular expressions so I need some help. How to fix following URL mistakes using RegEx?
- https:/
- https/
- https//
- http//
- http:/
Im not good at regular expressions so I need some help. How to fix following URL mistakes using RegEx?
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