PHP Regular Expression (RegEx) #1

in #utopian-io7 years ago (edited)

What Will I Learn?

  • PHP RegEx

Requirements

  • PHP
  • Linux
  • Notepad

Difficulty

  • Intermediate

Tutorial Contents

The Regular Expression (RegEx) library is an expression that recognizes complex strings. It is used to check whether the strings are in the correct format, or to inquire as to whether the strings have the desired meaning. In this section we will learn how to create regular expressions and how to use them together with the PHP language.

Basic Concepts

Regular expressions are a difficult issue for beginners to learn. For this reason, we will proceed step by step starting from the most basic to advanced regular expressions.

PHP and RegEx

The core functions that you can use regular expressions in the PHP programming language are listed in the following tablature.

Function NameTask
preg_match()The pattern is applied to the text only once. If the result is found, the result is returned as a sequence variable. Otherwise it will return false.
preg_match_all()The pattern is applied to the entire text line (many times). Desene rotates all the results by adding them to the liner. If no result is found, it returns false.
preg_replace()It works like the str_replace () function in the PHP language. The difference between them The str_replace() function searches for a constant value, and preg_replace() looks for and replaces regularly validating text strings.
preg_split()It works like the split() or explode() functions in the PHP language. The difference is that the preg_split() function can be treated as a regular delimiter when the split() or explode() functions take a constant value as a delimiter.
preg_grep()This function is used with array elements. It applies regular expression to all elements of the string and returns the result as an array.
preg_quote()It examines all the special characters that can cause the error in the text string to be queried by regular expression.
Simple Matches

We can question whether a fixed expression exists in the text strings. The most basic regular expressions are in the following tablature.

Regular ExpressionExplanation
abcInside abc matches all passing text strings.
^abcMatches only text strings that start with abc (^=text start sign)
abc$Matches only the text strings that end with abc. ($=end of text mark)
^abc$Matches only abc.

In order to understand the basic subject, I would like to immediately sample the above statements. When using regular expressions in PHP language, you should not choose the function that handles the pattern appropriately. For now I want to start with the most basic function.

In this section we will use the reg_match() function to match the regular expression with the text field. Now we will use this function with 3 parameters. Now we will use our pattern (regular expression), the text string to match the pattern with the second parameter, we will send the change that results when the pairing is done. The results will be rotated in a series.

<?php
$pattern = '#abc#' `;
$text = 'xyzabcdef';
preg_match($pattern, $text, $result);
print_r($result);
?>

In this example, it is questioned whether abc value exists anywhere in the text string. The diez (#) sign in the pattern is a delimiter and it is necessary to use it. Different delimiter characters can be used provided that it is alpha numeric. When the above commands are executed, the matching expression will be returned in the $result array variable because the abc value in the text exists.

<?php 
$pattern = '#^abc#' ;
$text = 'xyzabcdef' ;
preg_match ($pattern, $text, $result);
print_r ($result) ;
?>

In the example above, only text strings beginning with abc are accepted. Note that the $result variable will not return any value, because we have defined the $text variable and the text field is not in the desired format.

Character Classes

To create sophisticated regular expressions, we need to define the regular expression between square brackets ([]). Let's examine the following table.

Regular ExpressionExplanation
[abc]Matches a, b, or c.
[a-z]Matches any lowercase letter.
[A-Z]Matches any capital letter.
[A-F]Matches any uppercase letter between A and F.
[a-z0-9]Matches any lowercase letter or number.
[a-z-.]Matches any lowercase letter, hyphen, or period.

Regular expressions can be prepared for text strings that are not specific, but whose rules are known, as can be understood from the definitions given above. As we have not seen the repeaters in regular expressions at the moment, we will perform a single letter query.

Note:

Regular expressions are suggested to be used in conjunction with the escape mark when it is expressed in the pattern because some special signs such as hyphens, pluss, question marks, and dot marks are separate tasks. So, if you want to create a regular expression that you want to match any lowercase and hyphen, try to define it as [a-z \ -].

When a pattern such as [a-z] is defined in regular expressions, the pattern matches any lower-case letter found only in the English alphabet. If you want to recognize Turkish characters, you need to develop the expression like [a-zçğıöşü].

<?php
$pattern = '#[a-h0-4]#';
$text = 'php7.1' ;
preg_match_all ($pattern, $text, $result);
print_r ($result);
?>

In the above example, php7.1 value has been loaded into the text string and the generated pattern is; All small weekdays between a-h and all numbers between 0-4 are defined. We used preg_match_all() instead of preg_match()** to query with PHP.

This function will return all matching values as you would guess. That is, the preg_match() function will return the first value that the match has made, and the preg_match_all() function will return all the values that the match was made.

The first topic of our Regex series is here. In the second location, we will examine some of the more complicated operators and some of the more sophisticated operators. Thank you for reading.

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

@bedis, Like your contribution, upvote.

Your contribution cannot be approved because it does not follow the Utopian Rules.

Violated Rule:

  • Tutorials must be technical instructions that teach non-trivial aspects of an Open Source project.

  • Design or video editing related tutorials, gameplay, simple on-screen instructions, ubiquitous functions (Save, Open, Print, etc.) or basic programming concepts (variables, operators, loops, etc.) will not be accepted.

My Opinion:

  • Trivial programming concepts.

  • A tutorial must be informative and explanatory, but also "tutor". This tutorial lacks "tutor"ing, and it is nothing more than an explanation of a documentation.

You can contact us on Discord.

[utopian-moderator]

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.029
BTC 58118.57
ETH 2462.81
USDT 1.00
SBD 2.38