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 -

Python Tornado package error when running server -

Qt QGraphicsScene is not accessable from QGraphicsView (on Qt 5.6.1) -