PHP preg_replace tutorials and examples

PHP regex – match any digit

Php regex can use digit shorthand (\d) to match a digit (0-9). To match any non digit character, regex shorthand \D (uppercase backslash D) can read more

PHP regex – match word character (letter, number or underscore)

Php regex can use word shorthand \w (lowercase backslash w) to match a word character. It matches with any of the following: letters (a-z,A-Z) Numbers read more

PHP regex – whitespace shorthand (\s) regex examples

PHP regex whitespace shorthand \s can be used to match whitespace characters. These are the characters it will match. Char Dec val Oct val tab read more

PHP regex – word boundary examples

Php regex can use word boundary character (\b) to match a word boundary. Beginning of string if following by a word character will also match read more

PHP – Regex OR (alternation) examples using pipe

Regex OR (alternation) in php can be matched using pipe (|) match character. This is very frequently used in regular expressions. Following are some ways read more

PHP – preg_replace back reference (in replacement text) examples

Sometimes we need to use back reference in PHP preg_replace to put back the matched pattern. One example can be if you want to append read more

Php regex delimiter examples

PHP requires regex patterns to have valid delimiters around it. Some languages may not require delimiters (e.g. python). To make code more readable, sometimes we read more

PHP beginning and end of string regex examples

Php regex can use caret (^) for matching from beginning of string and dollar ($) for end of line. Here are some examples using preg_replace. read more

PHP remove non printable characters from a string

Regex to remove non printable characters from a string Space is first printable char and tilde (~) is last printable ascii char. We replace any read more

Php look ahead and look behind regex examples

Look ahead and look behind can be very useful to match a pattern which is followed, not followed, preceded or preceded by a certain pattern. read more