Regex to match an IP address [closed]

Don’t use a regex when you don’t need to 🙂 $valid = filter_var($string, FILTER_VALIDATE_IP); Though if you really do want a regex… $valid = preg_match(‘/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/’, $string); The regex however will only validate the format, the max for any octet is the max for an unsigned byte, or 255. This is why IPv6 is necessary – … Read more

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

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”; }

PHP preg_match to find whole words

preg_match(‘/\b(express\w+)\b/’, $string, $matches); // matches expression preg_match(‘/\b(\w*form\w*)\b/’, $string, $matches); // matches perform, // formation, unformatted Where: \b is a word boundary \w+ is one or more “word” character* \w* is zero or more “word” characters See the manual on escape sequences for PCRE. * Note: although not really a “word character”, the underscore _ is … 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

PHP – regex to allow letters and numbers only

1. Use PHP’s inbuilt ctype_alnum You dont need to use a regex for this, PHP has an inbuilt function ctype_alnum which will do this for you, and execute faster: <?php $strings = array(‘AbCd1zyZ9’, ‘foo!#$bar’); foreach ($strings as $testcase) { if (ctype_alnum($testcase)) { echo “The string $testcase consists of all letters or digits.\n”; } else { … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)