How to validate a domain name using Regex & PHP?

The accepted answer is incomplete/wrong. The regex pattern; should NOT validate domains such as: -example.com, example–.com, -example-.-.com, example.000, etc… should validate domains such as: schools.k12, newTLD.clothing, good.photography, etc… After some further research; below is the most correct, cross-language and compact pattern I could come up with: ^(?!\-)(?:(?:[a-zA-Z\d][a-zA-Z\d\-]{0,61})?[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$ This pattern conforms with most* of the rules … Read more

Are the PHP preg_functions multibyte safe?

pcre supports utf8 out of the box, see documentation for the ‘u’ modifier. Illustration (\xC3\xA4 is the utf8 encoding for the german letter “ä”) echo preg_replace(‘~\w~’, ‘@’, “a\xC3\xA4b”); this echoes “@@¤@” because “\xC3” and “\xA4” were treated as distinct symbols echo preg_replace(‘~\w~u’, ‘@’, “a\xC3\xA4b”); (note the ‘u’) prints “@@@” because “\xC3\xA4” were treated as a … Read more

My pattern isn’t matching a ISO style date, why? [duplicate]

Why use regex? Use DateTime class. function validateDate($date, $format=”Y-m-d H:i:s”) { $d = DateTime::createFromFormat($format, $date); return $d && $d->format($format) == $date; } You can use this function for all kind of date/time validations. Examples: var_dump(validateDate(‘2012-02-28 12:12:12’)); # true var_dump(validateDate(‘2012-02-30 12:12:12’)); # false var_dump(validateDate(‘2012-02-28’, ‘Y-m-d’)); # true var_dump(validateDate(’28/02/2012′, ‘d/m/Y’)); # true var_dump(validateDate(’30/02/2012′, ‘d/m/Y’)); # false var_dump(validateDate(’14:50′, … Read more

Unknown modifier ‘/’ in …? what is it? [duplicate]

I assume $value contains a slash /, which is not escaped by preg_quote: The special regular expression characters are: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : – Pass the delimiter you use to the function: preg_match_all(“/[^\s]*”.preg_quote($value, “https://stackoverflow.com/”).”[^\s]*/iu”, $row_search[‘content’], $final_matched); // —^ or use … Read more

Function eregi() is deprecated [duplicate]

preg_match expects its regex argument to be within a pair delimiters. So try: if ( ! preg_match(“#convert$#i”, $this->library_path)) { if ( ! preg_match(“#/$#i”, $this->library_path)) $this->library_path .= “https://stackoverflow.com/”; $this->library_path .= ‘convert’; } if (preg_match(“#gd2$#i”, $protocol)) { $protocol=”image_process_gd”; }