PHP Programming/Regular expressions

Syntax

 * : ignore the capture group when numeration. Ex:
 * : negation. Ex:
 * : first capture group result.

Attention: to search for a dollar,  doesn't work because it's the variables format, so the simple quotes must be used instead of the double quotes:.

in PHP, the regex patterns must always be surrounded by a delimiter symbol. We generally use the grave accent (`), but we also find / and #.

In addition, we can add some options after these delimiters:

Research
The function, which allowed to research in regex, has been replaced by   since PHP 5.3.

preg_match
The function is the main regex search function. It returns a Boolean and asks the two mandatory parameters: the regex pattern and the string to scan.

The third parameter represents the variable which stores the results array.

Finally, the fourth accepts an PHP flag allowing to modify the function base behavior.


 * Minimal example:


 * Advanced example:

Flag examples:
 * PREG_OFFSET_CAPTURE: displays the searched substring position in the string.
 * PREG_GREP_INVERT: displays the inverse in.

preg_grep
This function searches into arrays.

preg_match_all
To get all true results in one array, replace preg_match by preg_match_all, and print by print_r.

Example to filter a file content:

preg_replace
The function preg_replace accepts three parameters: the replaced and replacing string to treat.

preg_filter
Same as  but its result only include the replacements.

preg_split
Decomposes a string.