diff --git a/languages/php.php b/languages/php.php index e1d9054..84bb1db 100644 --- a/languages/php.php +++ b/languages/php.php @@ -331,7 +331,6 @@ abstract class AbstractClassName } - /** * Basic Implementation of LoggerAwareInterface. * @see https://github.com/php-fig/log/blob/master/Psr/Log/LoggerAwareTrait.php @@ -366,3 +365,106 @@ class ClassWithLogger */ use LoggerAwareTrait; } + + +/** + * PHP Regex. + */ + +// Meta Characters. + +^ Start of subject (or line in multiline mode) +$ End of subject (or line in multiline mode) +[ Start character class definition +] End character class definition +| Alternates, eg (a|b) matches a or b +( Start subpattern +) End subpattern +\ Escape character + +// Pattern Modifiers. + +i Caseless - ignore case +m Multiline mode - ^ and $ match start and end of lines +s Dotall - . class includes newline +x Extended- comments & whitespace +e preg_replace only - enables evaluation of replacement as PHP code +S Extra analysis of pattern +U Pattern is ungreedy +u Pattern is treated as UTF-8 + +// Subpattern Modifiers & Assertions. +(?:) Non capturing subpattern ((?:foo|fu)bar) matches foobar or fubar without foo or fu appearing as a captured subpattern +(?=) Positive look ahead assertion foo(?=bar) matches foo when followed by bar +(?!) Negative look ahead assertion foo(?!bar) matches foo when not followed by bar +(?<=) Positive look behind assertion (?<=foo)bar matches bar when preceded by foo +(?) Once-only subpatterns (?>\d+)bar Performance enhancing when bar not present +(?(x)) Conditional subpatterns (?(3)foo|fu)bar Matches foo if 3rd subpattern has matched, fu if not +(?#) Comment (?# Pattern does x y or z) + +// Base Character Classes +\w Any "word" character (a-z 0-9 _) +\W Any non "word" character +\s Whitespace (space, tab CRLF) +\S Any non whitepsace character +\d Digits (0-9) +\D Any non digit character +. (Period) - Any character except newline + +// Multiplicity. +n* Zero or more of n +n+ One or more of n +n? Zero or one occurrences of n +{n} n occurrences exactly +{n,} At least n occurrences +{,m} At most m occurrences +{n,m} Between n and m occurrences (inclusive) + + +// PHP Regular Expression Functions. + +Function Description +preg_match() The preg_match() function searches string for pattern, returning true if pattern exists, and false otherwise. +preg_match_all() The preg_match_all() function matches all occurrences of pattern in string. Useful for search and replace. +preg_replace() The preg_replace() function operates just like ereg_replace(), except that regular expressions can be used in the pattern and replacement input parameters. +preg_split() Preg Split (preg_split()) operates exactly like the split() function, except that regular expressions are accepted as input parameters. +preg_grep() The preg_grep() function searches all elements of input_array, returning all elements matching the regex pattern within a string. +preg_ quote() Quote regular expression characters + +// Code Snippets. + +//A better solution for validate email syntax is using filter_var. +if (filter_var('test+email@fexample.com', FILTER_VALIDATE_EMAIL)) { + echo "Your email is ok."; +} else { + echo "Wrong email address format."; +} + +//Validate username, consist of alpha-numeric (a-z, A-Z, 0-9), underscores, and has minimum 5 character and maximum 20 character. +//You could change the minimum character and maximum character to any number you like. +$username = "user_name12"; +if (preg_match('/^[a-z\d_]{5,20}$/i', $username)) { + echo "Your username is ok."; +} else { + echo "Wrong username format."; +} + +//Validate domain +$url = "http://domain-name.com/"; +if (preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $url)) { + echo "Your url is ok."; +} else { + echo "Wrong url."; +} + +//Extract domain name from certain URL +$url = "http://domain-name.com/index.html"; +preg_match('@^(?:http://)?([^/]+)@i', $url, $matches); +$host = $matches[1]; +echo $host; // domain-name.com + +//Highlight a word in the content +$text = "A regular expression (shortened as regex) is a sequence of characters that define a search pattern. Usually such patterns are used by string-searching algorithms for 'find' or 'find and replace' operations on strings, or for input validation."; +$text = preg_replace("/\b(regex)\b/i", 'replaced content', $text); +echo $text; /*A regular expression (shortened as replaced content) is a sequence of characters that define a search pattern. Usually such patterns are used by string-searching algorithms for 'find' or 'find and replace' operations on strings, or for input validation.*/