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

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -