PHP REGEX match a domain name from a given Url -


what want given domain present in string or not.

examples of problem are

+----------------------------------------------+-----------------------+ | input                                        | output                | +----------------------------------------------+-----------------------+ | http://www.example.com/questions/ask         | match or true         | | http://example.com/check                     | match or true         | | http://www.google.com/ig/moduleurl           | |    =http%3a%2f%2fwww.example.com%2fquestion  | false                 | | http://example.com/search/%25c3%25a9t%25     | match true            | +----------------------------------------------+-----------------------+ 

any appreciable

thanks

no need regex here imo:

using parse_url() check man here, can domain, host... want, really. coupled (extremely fast) string functions:

if (strstr(parse_url($input,php_url_host),'example.com')) {     echo $input.' match'; } 

but quickest way in scenario be:

$match = strpos($input, 'example.com'); $match = $match !== false && $match <= 12 ? true : false; //12 max https://www.example.com 

you wouldn't need !!(...);, that's can se $match being assigned boolean

but first suggestion still looks cleaner , more readable, eye.

if string beginning host you're looking isn't valid either:

$match = strpos($input, 'example.com'); $match = !!($match && $match < 13); 

is fastest approach can think of


Comments