- Test the PHP function preg_match
- preg_match — online function
- Preg match cheatsheet
- Stay informed
- preg_match_all
- Parameters
- Return Values
- Exceptions and Errors
- Changelog
- Related Functions
- preg_match_all
- Parameters
- Return Values
- Exceptions and Errors
- Changelog
- Related Functions
- preg_match
- Parameters
- Return Values
- Exceptions and Errors
- Notes
- Changelog
- Related Functions
Test the PHP function preg_match
This PHP preg_match online tool is design for you to try patterns on an input.
preg_match is a PHP function that test a pattern to a string or a text set in input.
The return will be a boolean value true or false . The pattern is a regular expression. Matches can be return by the function in a variable.
Our tool will show you both results : the boolean and the matched value.
It will help you to test a pattern without having to update your script several time. It is easy to use. You just have to copy the input and the pattern in the form and hit the button.
About patterns here are some information which may help you to build one :
Element | Description |
---|---|
// | Start and end delimiters |
^ | Meaning that the input has to start at this point of the pattern |
$ | Meaning that the input has to end at this point of the pattern |
(.*?) | Matches pretty much all the values possibles at this place, I actually use it a lot when I don’t have to worry about unsafe values. |
(6) | One digit between 0 and 9 |
(4) | 3 digits between 0 and 9. You just have to change the 3 buy any length. |
(1*) | All digits between 0 and 9, it is an unlimited length. |
([a-zA-Z]*) | All alphabetic strings |
Copyright 2023 — Contact us — 2023-07-21 16:00:13
preg_match — online function
Perform a regular expression match. Use /g option (global match) to use function like preg_match_all. This tool it’s a javascript function so result is generated by your browser and it’s not sent across internet .
Preg match cheatsheet
Pattern | Description |
---|---|
[a-z]+ | Any string with small letters only |
[A-Z]+ | Any string with uppercase letters only |
[abc] | Single letter a, b or c |
[abc]$ | Single letter a, b or c at the end of the line |
[abc] | Single letter a, b or c occurring at least twice |
[abc] | Single letter a, b or c occurring at least twice and a maximum 5 |
^ | Begining of the line |
$ | End of the line |
\A | Begining of string |
\z | End of string |
. | Any char |
\s | Any whitespace char |
\S | Any non whitespace char |
\d | Any digit |
\D | Any non-digit sign |
\w | Any word character (letter, number, underscore) |
\W | Any non-word character |
\b | Boundary character. For string: «This island is beautiful» if we use regex: \bis\b despite three occurences of is, only one will be found. This and island occurences are skipped: Th is is is beautiful. |
I’m create articles, tools so I can learn english and practice programming here. I hope some of this stuff can help you and save your time. If you have any suggestions, fell free to contact me.
Stay informed
Be the first to know new content and sign up for Scriptun newsletter.
preg_match_all
Searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags .
After the first match is found, the subsequent searches are continued on from end of the last match.
Parameters
The pattern to search for, as a string.
Array of all matches in multi-dimensional array ordered according to flags .
Can be a combination of the following flags (note that it doesn’t make sense to use PREG_PATTERN_ORDER together with PREG_SET_ORDER ): 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.
preg_match_all ( «|<[^>]+>(.*)[^>]+>|U» ,
«example:
» ,
$out , PREG_PATTERN_ORDER );
echo $out [ 0 ][ 0 ] . «, » . $out [ 0 ][ 1 ] . «\n» ;
echo $out [ 1 ][ 0 ] . «, » . $out [ 1 ][ 1 ] . «\n» ;
?>?php
The above example will output:
So, $out[0] contains array of strings that matched full pattern, and $out[1] contains array of strings enclosed by tags.
If the pattern contains named subpatterns, $matches additionally contains entries for keys with the subpattern name.
If the pattern contains duplicate named subpatterns, only the rightmost subpattern is stored in $matches[NAME] .
The above example will output:
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.
preg_match_all ( «|<[^>]+>(.*)[^>]+>|U» ,
«example:
» ,
$out , PREG_SET_ORDER );
echo $out [ 0 ][ 0 ] . «, » . $out [ 0 ][ 1 ] . «\n» ;
echo $out [ 1 ][ 0 ] . «, » . $out [ 1 ][ 1 ] . «\n» ;
?>?php
The above example will output:
If this flag is passed, for every occurring match the appendant string offset (in bytes) will also be returned. Note that this changes the value of matches into an array of arrays where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
preg_match_all ( ‘/(foo)(bar)(baz)/’ , ‘foobarbaz’ , $matches , PREG_OFFSET_CAPTURE );
print_r ( $matches );
?>?php
The above example will output:
If this flag is passed, unmatched subpatterns are reported as null ; otherwise they are reported as an empty string .
If no order flag is given, PREG_PATTERN_ORDER is assumed.
Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search (in bytes).
Note:
Using offset is not equivalent to passing substr($subject, $offset) to preg_match_all in place of the subject string, because pattern can contain assertions such as ^, $ or (?<=x). See preg_match for examples.
Return Values
Returns the number of full pattern matches (which might be zero), or false on failure.
Exceptions and Errors
If the regex pattern passed does not compile to a valid regex, an E_WARNING is emitted.
Changelog
Version | Description |
7.2.0 | The PREG_UNMATCHED_AS_NULL is now supported for the $flags parameter. |
Related Functions
- preg_last_error — Returns the error code of the last PCRE regex execution
- preg_match — Perform a regular expression match
- preg_quote — Quote regular expression characters
- preg_replace — Perform a regular expression search and replace
- preg_split — Split string by a regular expression
preg_match_all
Searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags .
After the first match is found, the subsequent searches are continued on from end of the last match.
Parameters
The pattern to search for, as a string.
Array of all matches in multi-dimensional array ordered according to flags .
Can be a combination of the following flags (note that it doesn’t make sense to use PREG_PATTERN_ORDER together with PREG_SET_ORDER ): 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.
preg_match_all ( «|<[^>]+>(.*)[^>]+>|U» ,
«example:
» ,
$out , PREG_PATTERN_ORDER );
echo $out [ 0 ][ 0 ] . «, » . $out [ 0 ][ 1 ] . «\n» ;
echo $out [ 1 ][ 0 ] . «, » . $out [ 1 ][ 1 ] . «\n» ;
?>?php
The above example will output:
So, $out[0] contains array of strings that matched full pattern, and $out[1] contains array of strings enclosed by tags.
If the pattern contains named subpatterns, $matches additionally contains entries for keys with the subpattern name.
If the pattern contains duplicate named subpatterns, only the rightmost subpattern is stored in $matches[NAME] .
The above example will output:
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.
preg_match_all ( «|<[^>]+>(.*)[^>]+>|U» ,
«example:
» ,
$out , PREG_SET_ORDER );
echo $out [ 0 ][ 0 ] . «, » . $out [ 0 ][ 1 ] . «\n» ;
echo $out [ 1 ][ 0 ] . «, » . $out [ 1 ][ 1 ] . «\n» ;
?>?php
The above example will output:
If this flag is passed, for every occurring match the appendant string offset (in bytes) will also be returned. Note that this changes the value of matches into an array of arrays where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
preg_match_all ( ‘/(foo)(bar)(baz)/’ , ‘foobarbaz’ , $matches , PREG_OFFSET_CAPTURE );
print_r ( $matches );
?>?php
The above example will output:
If this flag is passed, unmatched subpatterns are reported as null ; otherwise they are reported as an empty string .
If no order flag is given, PREG_PATTERN_ORDER is assumed.
Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search (in bytes).
Note:
Using offset is not equivalent to passing substr($subject, $offset) to preg_match_all in place of the subject string, because pattern can contain assertions such as ^, $ or (?<=x). See preg_match for examples.
Return Values
Returns the number of full pattern matches (which might be zero), or false on failure.
Exceptions and Errors
If the regex pattern passed does not compile to a valid regex, an E_WARNING is emitted.
Changelog
Version | Description |
7.2.0 | The PREG_UNMATCHED_AS_NULL is now supported for the $flags parameter. |
Related Functions
- preg_last_error — Returns the error code of the last PCRE regex execution
- preg_match — Perform a regular expression match
- preg_quote — Quote regular expression characters
- preg_replace — Perform a regular expression search and replace
- preg_split — Split string by a regular expression
preg_match
Searches subject for a match to the regular expression given in pattern .
Parameters
The pattern to search for, as a string.
If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.
flags can be a combination of the following flags: PREG_OFFSET_CAPTURE
If this flag is passed, for every occurring match the appendant string offset (in bytes) will also be returned. Note that this changes the value of matches into an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
preg_match ( ‘/(foo)(bar)(baz)/’ , ‘foobarbaz’ , $matches , PREG_OFFSET_CAPTURE );
print_r ( $matches );
?>?php
The above example will output:
If this flag is passed, unmatched subpatterns are reported as null ; otherwise they are reported as an empty string .
preg_match ( ‘/(a)(b)*(c)/’ , ‘ac’ , $matches );
var_dump ( $matches );
preg_match ( ‘/(a)(b)*(c)/’ , ‘ac’ , $matches , PREG_UNMATCHED_AS_NULL );
var_dump ( $matches );
?>?php
The above example will output:
Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search (in bytes).
Note:
Using offset is not equivalent to passing substr($subject, $offset) to preg_match in place of the subject string, because pattern can contain assertions such as ^, $ or (?<=x). Compare:
$subject = «abcdef» ;
$pattern = ‘/^def/’ ;
preg_match ( $pattern , $subject , $matches , PREG_OFFSET_CAPTURE , 3 );
print_r ( $matches );
?>?php
The above example will output:
$subject = «abcdef» ;
$pattern = ‘/^def/’ ;
preg_match ( $pattern , substr ( $subject , 3 ), $matches , PREG_OFFSET_CAPTURE );
print_r ( $matches );
?>?php
Alternatively, to avoid using substr, use the \G assertion rather than the ^ anchor, or the A modifier instead, both of which work with the offset parameter.
Return Values
preg_match returns 1 if the pattern matches given subject , 0 if it does not, or false on failure.
Warning:
This function may return Boolean false , but may also return a non-Boolean value which evaluates to false . Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
Exceptions and Errors
If the regex pattern passed does not compile to a valid regex, an E_WARNING is emitted.
Notes
Do not use preg_match if you only want to check if one string is contained in another string. Use strpos instead as it will be faster.
Changelog
Version | Description |
7.2.0 | The PREG_UNMATCHED_AS_NULL is now supported for the $flags parameter. |
Related Functions
- preg_last_error_msg — Returns the error message of the last PCRE regex execution
- preg_last_error — Returns the error code of the last PCRE regex execution
- preg_match_all — Perform a global regular expression match
- preg_quote — Quote regular expression characters
- preg_replace — Perform a regular expression search and replace
- preg_split — Split string by a regular expression