String regular expression in php

PHP | Regular Expressions

Most programming languages involves data entry, developers frequently run into issues when data is gathered in free text fields. In today’s application programming, regular expressions are practically ubiquitous. A popular open-source scripting language called PHP. Text, HTML, CSS, JavaScript, and PHP code can all be found in PHP files. When searching for or replacing a string with a pattern, developers frequently use regular expressions, which are a fundamental component of PHP. This search pattern can be used to specify your search criteria when looking for information within a text. A regular expression can be a simple pattern or just one character.
All kinds of text search and text replacement operations can be carried out using regular expressions. This lesson will teach you how regular expressions function as well as how to effectively use them in PHP for pattern matching. Let’s dive into the world of regular expressions now, if you’re ready.

What are Regular Expressions

Regular Expressions, also referred to as «regex» or «RegExp,» are formatted text strings that are used to search for patterns in text. A search pattern is made up of a string of characters. An efficient technique to describe a string pattern that matches a specific amount of text is through the use of regular expressions. You probably already know that PHP, an open-source language frequently used for building websites, offers regular expression functions as a crucial tool.
Numerous other programming languages use regular expressions in a similar way to PHP. The same is true for other apps that offer regexes with different syntaxes. Regexes are used on huge files and strings by numerous contemporary languages and technologies. One of the most potent techniques for effective and efficient text processing and manipulation nowadays is the use of regular expressions. For instance, it can be used to find or replace matching strings within text content, check the accuracy of data format (such as name, email, phone number, etc.) entered by the user, and more. They merely define a specific search pattern as a text string and are nothing more than a pattern or a series of characters. They lay the groundwork for capabilities related to pattern matching. You can replace one string with another string, split a string into multiple chunks, and search for a certain string inside another string using regular expression. You can look for a specific string inside another string using regular expression. We can also split a string into many parts and replace one string with another string. They construct complex expressions by using the arithmetic operators
(+, -, ^ ). They may assist you with things like validating IP addresses and email addresses, among others.

Читайте также:  Python считать xml файл

Advantages of Regular Expressions

  1. Regular expressions aid programmers in text string validation.
  2. It provides a potent tool for pattern recognition, analysis, and text string modification.
  3. Simple ways are offered to find the patterns by employing regexes functions.
  4. Regexes aid in the development of the HTML tag recognition system.
  5. Regexes are frequently used for spam filtration, password strength verification, form validation, and browser detection.
  6. It is useful in user input validation testing for user inputs such as IP address, email address, and mobile number.
  7. It assists in emphasizing the file’s unique keywords based on the input or search result.
  8. Metacharacters let us design more intricate patterns
  9. Browser detection, spam filtration, determining the strength of passwords, and form validations are the main uses of regexes.

Operators in Regular Expressions

Let us look into some of the operators in PHP regular expressions.

## Operator ## Description
^ It denotes the start of the string.
$ It denotes the end of the string.
. It denotes almost any single character.
() It denotes a group of expressions.
[] It finds a range of characters for example [xyz] means x, y, or z.
[^] It finds the items which are not in range for example [^abc] meas NOT a, ,b or c.
– (dash) It finds for character range within the given item range for example [a-z] means a through z.
(pipe)
? It denotes zero or one of preceding character or item range.
* It denotes zero or more the of preceding character or item range.
+ It denotes one or more of preceding character or item range.
It denotes exactly n times of preceding character or item range for example n.
It denotes atleast n times of preceding character or item range for example n.
It denotes at least n but not more than m times for example n means 2 to 4 of n.
\ It denotes the escape character.

Create Regular Expressions

To create a regular expression, you place a pattern in forward-slashes like this:

Источник

PHP Regular Expressions

A regular expression is a sequence of characters that forms a search pattern. When you search for data in a text, you can use this search pattern to describe what you are searching for.

A regular expression can be a single character, or a more complicated pattern.

Regular expressions can be used to perform all types of text search and text replace operations.

Syntax

In PHP, regular expressions are strings composed of delimiters, a pattern and optional modifiers.

In the example above, / is the delimiter, w3schools is the pattern that is being searched for, and i is a modifier that makes the search case-insensitive.

The delimiter can be any character that is not a letter, number, backslash or space. The most common delimiter is the forward slash (/), but when your pattern contains forward slashes it is convenient to choose other delimiters such as # or ~.

Regular Expression Functions

PHP provides a variety of functions that allow you to use regular expressions. The preg_match() , preg_match_all() and preg_replace() functions are some of the most commonly used ones:

Function Description
preg_match() Returns 1 if the pattern was found in the string and 0 if not
preg_match_all() Returns the number of times the pattern was found in the string, which may also be 0
preg_replace() Returns a new string where matched patterns have been replaced with another string

Using preg_match()

The preg_match() function will tell you whether a string contains matches of a pattern.

Example

Use a regular expression to do a case-insensitive search for «w3schools» in a string:

Using preg_match_all()

The preg_match_all() function will tell you how many matches were found for a pattern in a string.

Example

Use a regular expression to do a case-insensitive count of the number of occurrences of «ain» in a string:

$str = «The rain in SPAIN falls mainly on the plains.»;
$pattern = «/ain/i»;
echo preg_match_all($pattern, $str); // Outputs 4
?>

Using preg_replace()

The preg_replace() function will replace all of the matches of the pattern in a string with another string.

Example

Use a case-insensitive regular expression to replace Microsoft with W3Schools in a string:

$str = «Visit Microsoft!»;
$pattern = «/microsoft/i»;
echo preg_replace($pattern, «W3Schools», $str); // Outputs «Visit W3Schools!»
?>

Regular Expression Modifiers

Modifiers can change how a search is performed.

Modifier Description
i Performs a case-insensitive search
m Performs a multiline search (patterns that search for the beginning or end of a string will match the beginning or end of each line)
u Enables correct matching of UTF-8 encoded patterns

Regular Expression Patterns

Brackets are used to find a range of characters:

Expression Description
[abc] Find one character from the options between the brackets
[^abc] Find any character NOT between the brackets
1 Find one character from the range 0 to 9

Metacharacters

Metacharacters are characters with a special meaning:

Metacharacter Description
| Find a match for any one of the patterns separated by | as in: cat|dog|fish
. Find just one instance of any character
^ Finds a match as the beginning of a string as in: ^Hello
$ Finds a match at the end of the string as in: World$
\d Find a digit
\s Find a whitespace character
\b Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b
\uxxxx Find the Unicode character specified by the hexadecimal number xxxx

Quantifiers

Quantifiers define quantities:

Quantifier Description
n+ Matches any string that contains at least one n
n* Matches any string that contains zero or more occurrences of n
n? Matches any string that contains zero or one occurrences of n
n Matches any string that contains a sequence of X n‘s
n Matches any string that contains a sequence of X to Y n‘s
n Matches any string that contains a sequence of at least X n‘s

Note: If your expression needs to search for one of the special characters you can use a backslash ( \ ) to escape them. For example, to search for one or more question marks you can use the following expression: $pattern = ‘/\?+/’;

Grouping

You can use parentheses ( ) to apply quantifiers to entire patterns. They also can be used to select parts of the pattern to be used as a match.

Example

Use grouping to search for the word «banana» by looking for ba followed by two instances of na:

Complete RegExp Reference

The reference contains descriptions and examples of all Regular Expression functions.

Источник

Оцените статью