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

What is happening when Matlab is starting a "parallel pool"? -

php - Cannot override Laravel Spark authentication with own implementation -

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