Understanding Regular Expression (part 2)

Tram Ho

Hello everyone, this article I will continue to write the topic of Regular Expression , if you do not know what it is, please refer to this article. Today I will introduce you to some functions used with Regular Expression in PHP and some cases that we need Regex.

Some functions use Regex in PHP

In PHP we have a total of 9 PCRE functions to use

  • preg_filter – perform a search of regular and alternative results
  • preg_grep – returns an array of results that match the pattern
  • preg_last_error – returns the error code of the last execution of the regex PCRE
  • preg_match && preg_match_all – execute matching regular expressions, the difference between the lower part I will say more clearly
  • preg_quote – put a backslash before each character as a special character in a regular expression in the input string
  • preg_replace – executes a search for which elements match a regular expression and replaces it with the string we need.
  • preg_replace_callback – executes a search for which elements match the regular expression and replaces it with the string we need and the result is then used to perform another callback function. The final result returned is the input after it has replaced and passed through that callback function.
  • preg_split – splits strings with regular expressions

And according to his work experience, the most commonly used are 3 functions preg_match , preg_match_all and preg_replace .

preg_match

This function is used to check if the input data matches the regular expression string and returns the result. Syntax

Inside:

  • $ pattern is Banana Regex
  • $ subject is the string to match
  • $ match is the result returned, passed as a reference (can be left blank if not needed)
  • $ flags is a parameter that indicates where the string matches (can be left blank) by default. $ flag = 0
  • $ offset is a parameter that specifies the starting position of the match (can be left blank) by default $ offset = 0

I have a simple example as follows

2 parameters passed

3 parameters passed Same as the example above but the third parameter as follows

4 parameters passed

The same example above but this time I will return the position of the string match by passing additional parameter 4

Here 0 and 12 are the start of the string matching the pattern

5 parameters passed Also the example above but we will pass the 5th parameter

If we pass this 5th argument to 11, the implication is that the function will regex from position 11 of the input string.

preg_match_all

This function works by matching all the patterns in the string. Left parameters are exactly the same as the preg_match function I mentioned above.

Inside:

  • $ pattern is Banana Regex
  • $ subject is the string to match
  • $ match is the result returned, passed as a reference (can be left blank if not needed)
  • $ flags is a parameter that indicates where the string matches (can be left blank) by default. $ flag = 0
  • $ offset is a parameter that indicates the starting position of the match (can be left blank) default $ offset = 0 I have a simple example as follows:

If we use preg_match_all , the result is as follows

The difference between preg_match_all() and preg_match() is that preg_match_all() will match all matches in the string, and preg_match() will only match the first one it meets, and the next it won’t match anymore.

preg_replace

  • $ pattern is the regular expression you want to match
  • $ stringReplacement is the replacement string for the found result
  • $ subject is the string that you need to search and replace
  • $ limit is the limit of the number of string replacements, the default $ limit = -1 is unlimited (can be left blank)
  • $ count is the number of string replacements when using the function, used as a reference (can be left blank)

For example

Some of the examples

Validate Email

Validate URL

Blank

If you guys have a question segment like “My name is ___. I’m from ____”

The request for a post is that the text must contain a blank paragraph, which is a minimum of 3 underscores to pass, and a maximum of 5 blank.

Validate alpha

Get domain from URL

Conclude

So through a few things I have synthesized, I hope to help you understand somewhat more Regex and Regex functions in PHP. Thank you for reading my article.

Refer

https://www.php.net/manual/en/ref.pcre.php

Share the news now

Source : Viblo