Regex functions in php

A regular expression (regex) is a formalized string of characters that defines a search pattern, primarily used for pattern matching within text. This pattern can be employed to locate, match, and manage specific substrings within a larger body of text, allowing for sophisticated text processing tasks. Regex is widely utilized in various applications such as data validation, string parsing, and syntax highlighting.

In this article, we are going to explore the regex functions commonly used in PHP. Regular expressions, or regex, are powerful tools for pattern matching and text processing. PHP offers a variety of functions to work with regex, enabling developers to perform complex search and manipulation tasks on strings with ease. Below are some of the key regex functions in PHP:

1.preg_replace

The preg_replace function in PHP is used to perform a search for a pattern within a string and replace the matched pattern with a specified replacement string. In this example, we are going to replace the name “Manoj” with “Raj” regardless of how “Manoj” is spelled. The i flag indicates a case-insensitive search, and the ? indicates that the character before the question mark is optional.

<?php

$paragraph = "Listen Manoj, I'm not any of those guys. Ok, manj?";

$pattern = “/mano?j/i”;

$output = preg_replace($pattern, Raj”, $paragraph);

var_dump($output);

//string(66) "Listen Raj, I'm not any of those guys. Ok, Raj?"

2.preg_match

The preg_match function in PHP is used to perform a search within a string for a specific pattern defined by a regular expression. If the pattern is found in the string, preg_match returns 1. If no match is found, it returns 0.

<?php

$paragraph = "Listen Manoj, I'm not any of those guys. Ok, manoj?";

$pattern = “/mano?j/i”;

$output = preg_match($pattern, $paragraph, $matches);

var_dump($output);

//int(1)

var_dump($matches);

/** array(1) {
  [0]=>
  string(7) "Manoj"
} **/

The preg_match function can also be used to store the first match found in an array by specifying the third parameter. However, it only finds the first match and stops. This is useful when you are interested in the first occurrence of a pattern within a string.


3.preg_match_all

The preg_match_all function in PHP is used to perform a global search within a string for a pattern defined by a regular expression. It finds all matches and stores them in an array. If matches are found, it returns an integer indicating the number of matches. If no matches are found, it returns 0.

<?php

$paragraph = "Listen Manoj, I'm not any of those guys. I am a programmer. Ok, manoj?";

$pattern = “/mano?j/i”;

$output = preg_match_all($pattern, $paragraph, $matches);

var_dump($output);

//int(2)

var_dump($matches);

/** array(1) {
  [0]=>
  array(2) {
    [0]=>
    string(7) "Manoj"
    [1]=>
    string(6) "manoj"
  }
} **/

4.preg_split

The preg_split function in PHP is used to split a string by a pattern defined by a regular expression. The resulting segments are stored in an array. This function is particularly useful for dividing strings into smaller parts based on complex delimiters. In this below example the pattern "/\s*,\s*/i" effectively splits a string by commas, ignoring any surrounding whitespace. This is useful for parsing lists of items separated by commas where spaces may be inconsistently applied.

<?php

$tags = "PHP , Programming,   Laravel";

$pattern = "/\s*,\s*/i";

$output = preg_split($pattern, $tags);

var_dump($output);

/** array(3) {
  [0]=>
  string(3) "PHP"
  [1]=>
  string(11) "Programming"
  [2]=>
  string(7) "Laravel"
} **/

5.preg_grep

The preg_grep function in PHP searches an array for elements that match a given regular expression pattern and returns an array of matching elements. This is useful when you need to filter an array based on a specific pattern.

<?php

$comments = [
   "Manoj is trustworthy",
   "Regex is easy if you understand the characters meanings.",
   "Always trust manj"
];

$pattern = "/mano?j/i";

$output = preg_grep($pattern,$comments);

var_dump($output);

/**
array(2) {
  [0]=>
  string(22) "Manoj is trustworthy"
  [2]=>
  string(19) "Always trust manj"
}
**/

“I’ll be writing another article specifically focused on explaining the characters used within the regular expression pattern. This will help newcomers to better grasp the concepts involved. If you have any questions or need further assistance, feel free to reach out. Keep learning!”

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top