Skip to content

Instantly share code, notes, and snippets.

@ganey
Last active May 20, 2016 10:56
Show Gist options
  • Save ganey/f26756a1ab928bc882f4734222ee94c6 to your computer and use it in GitHub Desktop.
Save ganey/f26756a1ab928bc882f4734222ee94c6 to your computer and use it in GitHub Desktop.
Domain / url regex
<?php
// tested here: https://regex101.com/r/iC1uQ4/1
// as laravel validation rule: 'regex:/^((http|https)\:\/\/|)([\w]+\.)+\w+($|[\w\/\.]+$|[\w\/\.]+\?[\w\=\&\.]+$)/'
$re = "/^((http|https)\:\/\/|)([\w|\-]+\.)+\w+($|[\w\-\/\.]+$|[\w\/\.]+\?[\w\=\&\.]+$)/";
$str = "www.example.com/metropolitanlondon";
preg_match($re, $str, $matches);
/*
true www.example.com/metropolitanlondon
true www.example.com
true http://example.co.uk
true example.com//dwa/../dwd
true www.example.com/dawdwad/fsef/rdgdr_szg/gsedg?btfbftb=fgersg&gdsrzgrg=rsths.sfra
false xrftjcomjfcgyvhj
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment