In this tutorial, you’ll see how regular expressions work, we use some of the data validation tasks to demonstrate how we can use regular expressions to validate usernames, phone numbers, and postal codes.
Category: PHP
PHP is a scripting language that’s usually embedded or combined with HTML and has many excellent libraries that provide fast, customized access to DBMSs.
Word Boundaries in Regular Expression
How to mark word boundaries to “match a specific word only” using a regular expression.
Handling Errors in Regular Expressions
The regular expression functions in PHP do not throw exceptions, so if there is an error in the regular expression, what will you do? To handle regular expressions errors, PHP offers two functions : preg_last_error() and preg_last_error_msg().
The :.\+*?[^]$(){}=!<>|:-# letters are special characters that make regular expressions work. If you want to match one of these characters you need to write a backslash in front of that character in the pattern. For example, if you want to match a “+” character, you write “\+” in the pattern. You can escape special characters manually by placing a backslash in front of each character you want to match, or you can the use preg_quote() function to escape special characters automatically. In this tutorial, you’ll also learn a technique that helps you to avoid escaping special characters.
How to use preg_split() function to split strings based on regular expressions and how to use preg_grep() function to search through an entire array of strings looking for text that matches a regular expression.
Learn how to use preg_replace_callback_array() and preg_replace_callback() functions to perform a regular expression search and replace using callbacks.
Search and replace with preg_replace()
Learn how to use the preg_replace() function to replace all occurrences of a pattern (regular expression) with replacement and get the modified result.
The preg_match() function searches a string for a specific pattern, it finds only the first match. On the other hand, the preg_match_all() function finds all matches in the string.
Regular Expressions
A regular expression or regex is used for searching, editing, extracting and manipulating data. Using regular expressions (regex) you can verify if a specific string matches a given text pattern or to find out a set of characters from a sentence or large batch of characters.
Convert a delimited string into an array
How to parse a CSV, tab, space, or any other single-byte character delimited string into an array using the str_getcsv() function.