regex - Fixing common URL mistakes in PHP using regular expression -
im not @ regular expressions need help. how fix following url mistakes using regex?
- https:/
- https/
- https//
- http//
- http:/
you can use following code
$re = '/^(https?)(\:?\/?\/?)/'; $str = 'https:/ https/ https// http// http:/'; $subst = '\\1://'; $result = preg_replace($re, $subst, $str); echo "the result of substitution ".$result;
the regex
/^(https?)(:?/?/?)/
matches http/https in first group, every other possibility in 2nd group
replace 2nd group correct value every time
see demo
Comments
Post a Comment