Examples | |
---|---|
Example | Description |
[a-g] | Any lowercase letter from a through g |
[0-9] | Any digit (0 through 9), equivalent to \d |
[^A-Z] | No uppercase letters from A through Z |
(foo|bar) | Either "foo" or "bar" |
(?(condition)yes-pattern) (?(condition)yes-pattern|no-pattern) |
If the condition is satisfied, the yes-pattern is used; otherwise the no-pattern (if present) is used. |
Syntax | |
---|---|
Character | Description |
\ | Escape character |
^ | Beginning of subject (or line in multi-line mode), or negate the character class, if first character of a class |
$ | End of subject (or line in multi-line mode) |
. | Match any character except newline |
[ | Start character class definition |
] | End character class definition |
| | Start of alternative branch |
( | Start subpattern |
) | End subpattern |
? | Extends the meaning of (, also 0 or 1 quantifier, also quantifier minimizer |
* | 0 or more quantifier |
+ | 1 or more quantifier |
{ | Start min/max quantifier |
} | End min/max quantifier |
\d | Any decimal digit |
\D | Any character that is not a decimal digit |
\s | Any whitespace character |
\S | Any character that is not a whitespace character |
\w | Any "word" character |
\W | Any "non-word" character |
\n | Newline (hex 0A) |
\r | Carriage return (hex 0D) |
\t | Tab (hex 09) |
\xdd | Character with hex code dd (for example \x40 is another way of writing @) |
\ddd | Character with octal code ddd (for example \040 is another way of writing a space) |
PCRE Modifiers | |
---|---|
Modifier | Description |
i (PCRE_CASELESS) | If this modifier is set, letters in the pattern match both upper and lower case letters. |
m (PCRE_MULTILINE) |
By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline (unless D modifier is set). This is the same as Perl. When this modifier is set, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect. |
s (PCRE_DOTALL) | If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier. |
x (PCRE_EXTENDED) | If this modifier is set, whitespace data characters in the pattern are totally ignored except when escaped or inside a character class, and characters between an unescaped # outside a character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, and makes it possible to include comments inside complicated patterns. Note, however, that this applies only to data characters. Whitespace characters may never appear within special character sequences in a pattern, for example within the sequence (?( which introduces a conditional subpattern. |
e |
If this modifier is set, preg_replace() does normal substitution of backreferences in the replacement string, evaluates it as PHP code, and uses the result for replacing the search string. Single and double quotes are escaped by backslashes in substituted backreferences. Only preg_replace() uses this modifier; it is ignored by other PCRE functions. Note: This modifier was not available in PHP 3. |
A (PCRE_ANCHORED) | If this modifier is set, the pattern is forced to be "anchored", that is, it is constrained to match only at the start of the string which is being searched (the "subject string"). This effect can also be achieved by appropriate constructs in the pattern itself, which is the only way to do it in Perl. |
D (PCRE_DOLLAR_ENDONLY) | If this modifier is set, a dollar metacharacter in the pattern matches only at the end of the subject string. Without this modifier, a dollar also matches immediately before the final character if it is a newline (but not before any other newlines). This modifier is ignored if m modifier is set. There is no equivalent to this modifier in Perl. |
S | When a pattern is going to be used several times, it is worth spending more time analyzing it in order to speed up the time taken for matching. If this modifier is set, then this extra analysis is performed. At present, studying a pattern is useful only for non-anchored patterns that do not have a single fixed starting character. |
U (PCRE_UNGREEDY) | This modifier inverts the "greediness" of the quantifiers so that they are not greedy by default, but become greedy if followed by "?". It is not compatible with Perl. It can also be set by a (?U) modifier setting within the pattern or by a question mark behind a quantifier (e.g. .*?). |
X (PCRE_EXTRA) | This modifier turns on additional functionality of PCRE that is incompatible with Perl. Any backslash in a pattern that is followed by a letter that has no special meaning causes an error, thus reserving these combinations for future expansion. By default, as in Perl, a backslash followed by a letter with no special meaning is treated as a literal. There are at present no other features controlled by this modifier. |
u (PCRE_UTF8) | This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern strings are treated as UTF-8. This modifier is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32. UTF-8 validity of the pattern is checked since PHP 4.3.5. |
PREG Constants | |
---|---|
Constant | Description |
PREG_PATTERN_ORDER | Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on. This flag is only used with preg_match_all(). |
PREG_SET_ORDER | Orders results so that $matches[0] is an array of first set of matches, $matches[1] is an array of second set of matches, and so on. This flag is only used with preg_match_all(). |
PREG_OFFSET_CAPTURE | See the description of PREG_SPLIT_OFFSET_CAPTURE. This flag is available since PHP 4.3.0. |
PREG_SPLIT_NO_EMPTY | This flag tells preg_split() to return only non-empty pieces. |
PREG_SPLIT_DELIM_CAPTURE | This flag tells preg_split() to capture parenthesized expression in the delimiter pattern as well. This flag is available since PHP 4.0.5. |
PREG_SPLIT_OFFSET_CAPTURE | If this flag is set, for every occurring match the appendant string offset will also be returned. Note that this changes the return values in an array where every element is an array consisting of the matched string at offset 0 and its string offset within subject at offset 1. This flag is available since PHP 4.3.0 and is only used for preg_split(). |
Variable | Description |
---|---|
'PHP_SELF' |
The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar. The __FILE__ constant contains the full path and filename of the current (i.e. included) file. If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available. |
'argv' | Array of arguments passed to the script. When the script is run on the command line, this gives C-style access to the command line parameters. When called via the GET method, this will contain the query string. |
'argc' | Contains the number of command line parameters passed to the script (if run on the command line). |
'GATEWAY_INTERFACE' | What revision of the CGI specification the server is using; i.e. 'CGI/1.1'. |
'SERVER_NAME' | The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host. |
'SERVER_SOFTWARE' | Server identification string, given in the headers when responding to requests. |
'SERVER_PROTOCOL' | Name and revision of the information protocol via which the page was requested; i.e. 'HTTP/1.0'; |
'REQUEST_METHOD' |
Which request method was used to access the page; i.e. 'GET', 'HEAD', 'POST', 'PUT'. Note: PHP script is terminated after sending headers (it means after producing any output without output buffering) if the request method was HEAD. |
'REQUEST_TIME' | The timestamp of the start of the request. Available since PHP 5.1.0. |
'QUERY_STRING' | The query string, if any, via which the page was accessed. |
'DOCUMENT_ROOT' | The document root directory under which the current script is executing, as defined in the server's configuration file. |
'HTTP_ACCEPT' | Contents of the Accept: header from the current request, if there is one. |
'HTTP_ACCEPT_CHARSET' | Contents of the Accept-Charset: header from the current request, if there is one. Example: 'iso-8859-1,*,utf-8'. |
'HTTP_ACCEPT_ENCODING' | Contents of the Accept-Encoding: header from the current request, if there is one. Example: 'gzip'. |
'HTTP_ACCEPT_LANGUAGE' | Contents of the Accept-Language: header from the current request, if there is one. Example: 'en'. |
'HTTP_CONNECTION' | Contents of the Connection: header from the current request, if there is one. Example: 'Keep-Alive'. |
'HTTP_HOST' | Contents of the Host: header from the current request, if there is one. |
'HTTP_REFERER' | The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted. |
'HTTP_USER_AGENT' | Contents of the User-Agent: header from the current request, if there is one. This is a string denoting the user agent being which is accessing the page. A typical example is: Mozilla/4.5 [en] (X11; U; Linux 2.2.9 i586). Among other things, you can use this value with get_browser() to tailor your page's output to the capabilities of the user agent. |
'HTTPS' | Set to a non-empty value if the script was queried through the HTTPS protocol. |
'REMOTE_ADDR' |
The Host name from which the user is viewing the current page. The reverse dns lookup is based off the REMOTE_ADDR of the user. Note: Your web server must be configured to create this variable. For example in Apache you'll need HostnameLookups On inside httpd.conf for it to exist. See also gethostbyaddr(). |
'REMOTE_PORT' | The port being used on the user's machine to communicate with the web server. |
'SCRIPT_FILENAME' |
The absolute pathname of the currently executing script. Note: If a script is executed with the CLI, as a relative path, such as file.php or ../file.php, $_SERVER['SCRIPT_FILENAME'] will contain the relative path specified by the user. |
'SERVER_ADMIN' | The value given to the SERVER_ADMIN (for Apache) directive in the web server configuration file. If the script is running on a virtual host, this will be the value defined for that virtual host. |
'SERVER_PORT' | The port on the server machine being used by the web server for communication. For default setups, this will be '80'; using SSL, for instance, will change this to whatever your defined secure HTTP port is. |
'SERVER_SIGNATURE' | String containing the server version and virtual host name which are added to server-generated pages, if enabled. |
'PATH_TRANSLATED' |
Filesystem- (not document root-) based path to the current script, after the server has done any virtual-to-real mapping. Note: As of PHP 4.3.2, PATH_TRANSLATED is no longer set implicitly under the Apache 2 SAPI in contrast to the situation in Apache 1, where it's set to the same value as the SCRIPT_FILENAME server variable when it's not populated by Apache. This change was made to comply with the CGI specification that PATH_TRANSLATED should only exist if PATH_INFO is defined. Apache 2 users may use AcceptPathInfo = On inside httpd.conf to define PATH_INFO. |
'SCRIPT_NAME' | Contains the current script's path. This is useful for pages which need to point to themselves. The __FILE__ constant contains the full path and filename of the current (i.e. included) file. |
'REQUEST_URI' | The URI which was given in order to access this page; for instance, '/index.html'. |
'PHP_AUTH_DIGEST' | When running under Apache as module doing Digest HTTP authentication this variable is set to the 'Authorization' header sent by the client (which you should then use to make the appropriate validation). |
'PHP_AUTH_USER' | When running under Apache or IIS (ISAPI on PHP 5) as module doing HTTP authentication this variable is set to the username provided by the user. |
'PHP_AUTH_PW' | When running under Apache or IIS (ISAPI on PHP 5) as module doing HTTP authentication this variable is set to the password provided by the user. |
'AUTH_TYPE' | When running under Apache as module doing HTTP authenticated this variable is set to the authentication type. |
Mode | Description |
---|---|
r | Open for reading only; place the file pointer at the beginning of the file. |
r+ | Open for reading and writing; place the file pointer at the beginning of the file. |
w | Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. |
w+ | Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. |
a | Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. |
a+ | Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. |
x | Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files. |
x+ | Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files. |
Constant | Description |
---|---|
__LINE__ | The current line number of the file. |
__FILE__ | The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path whereas in older versions it contained relative path under some circumstances. |
__FUNCTION__ | The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. |
__CLASS__ | The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. |
__METHOD__ | The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive). |
Comparison Operators | ||
---|---|---|
Example | Name | Result |
$a == $b | Equal | TRUE if $a is equal to $b. |
$a === $b | Identical | TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4) |
$a != $b | Not equal | TRUE if $a is not equal to $b. |
$a <> $b | Not equal | TRUE if $a is not equal to $b. |
$a !== $b | Not identical | TRUE if $a is not equal to $b, or they are not of the same type. (introduced in PHP 4) |
$a < $b | Less than | TRUE if $a is strictly less than $b. |
$a > $b | Greater than | TRUE if $a is strictly greater than $b. |
$a <= $b | Less than or equal to | TRUE if $a is less than or equal to $b. |
$a >= $b | Greater than or equal to | TRUE if $a is greater than or equal to $b. |
Comparison with Various Types | ||
---|---|---|
Type of Operand 1 | Type of Operand 2 | Result |
null or string | string | Convert NULL to "", numerical or lexical comparison |
bool or null | anything | Convert to bool, FALSE < TRUE |
object | object | Built-in classes can define its own comparison, different classes are uncomparable, same class - compare properties the same way as arrays (PHP 4), PHP 5 has its own explanation |
string, resource or number |
string, resource or number |
Translate strings and resources to numbers, usual math |
array | array | Array with fewer members is smaller, if key from operand 1 is not found in operand 2 then arrays are uncomparable, otherwise - compare value by value |
array | anything | array is always greater |
object | anything | object is always greater |
Comparisons of $x with PHP Functions | |||||
---|---|---|---|---|---|
Expression | gettype() | empty() | is_null() | isset() | boolean : if($x) |
$x == ""; | string | TRUE | FALSE | TRUE | FALSE |
$x == NULL; | NULL | TRUE | TRUE | FALSE | FALSE |
var $x; | NULL | TRUE | TRUE | FALSE | FALSE |
$x is undefined | NULL | TRUE | TRUE | FALSE | FALSE |
$x = array(); | array | TRUE | FALSE | TRUE | FALSE |
$x = false; | boolean | TRUE | FALSE | TRUE | FALSE |
$x = true; | boolean | FALSE | FALSE | TRUE | TRUE |
$x = 1; | integer | FALSE | FALSE | TRUE | TRUE |
$x = 42; | integer | FALSE | FALSE | TRUE | TRUE |
$x = 0; | integer | TRUE | FALSE | TRUE | FALSE |
$x = -1; | integer | FALSE | FALSE | TRUE | TRUE |
$x = "1" | string | FALSE | FALSE | TRUE | TRUE |
$x = "0" | string | TRUE | FALSE | TRUE | FALSE |
$x = "-1" | string | FALSE | FALSE | TRUE | TRUE |
$x = "php" | string | FALSE | FALSE | TRUE | TRUE |
$x = "true" | string | FALSE | FALSE | TRUE | TRUE |
$x = "false" | string | FALSE | FALSE | TRUE | TRUE |
Loose comparisons with == | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
TRUE | FALSE | 1 | 0 | -1 | "1" | "0" | "-1" | NULL | array() | "PHP" | |
TRUE | TRUE | FALSE | TRUE | FALSE | TRUE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE |
FALSE | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | TRUE | TRUE | FALSE |
1 | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE |
0 | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | TRUE | FALSE | TRUE |
-1 | TRUE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE |
"1" | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE |
"0" | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE |
"-1" | TRUE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE |
NULL | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | TRUE | TRUE | FALSE |
array() | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | TRUE | FALSE |
"PHP" | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE |
Strict comparisons with === | |||||||||||
TRUE | FALSE | 1 | 0 | -1 | "1" | "0" | "-1" | NULL | array() | "PHP" | |
TRUE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
1 | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
0 | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
-1 | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
"1" | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE |
"0" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE |
"-1" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE |
NULL | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE |
array() | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE |
"PHP" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE |