PHP

PHP Regular Expressions: preg_match, preg_split, preg_replace

Tuchpad

Regular expressions are a powerful pattern matching algorithm that can be performed in a single expression. Regular expressions use arithmetic operators such as (+, -, ^) to create complex expressions. Regular expressions help you perform tasks such as validating email addresses, IP addresses, and more.

Why use regular expressions

  • Regular expressions make it easy to identify patterns in string data by calling a single function. This saves us coding time.
  • When validating user input such as email address, domain names, phone numbers, IP addresses.
  • Highlight keywords in search results.
  • When creating a custom HTML template. Regular expressions can be used to identify template tags and replace them with actual data.

PHP Regular Expressions

PHP has built-in functions that allow us to work with regular expressions. Let’s now take a look at the commonly used regular expression functions in PHP.

preg_match – This function is used to match against a pattern in a string. It returns true if a match is found and false if no match is found.
preg_split – This function is used to match against a pattern in a string and then splits the results into a numeric array.
< code> preg_replace – This function is used to match against a string pattern and then replace the match with the specified text.
The following is the syntax for a regular expression function such as preg_match , preg_split or preg_replace :

<?php
function_name('/pattern/',subject);
?>

“Function_name (…)” is either preg_match, preg_split, or preg_replace.
“/ … /” The forward slash marks the beginning and end of our regular expression.
“/ Pattern /” is the pattern we need.
“Subject” is the text string to match against.

Let’s now take a look at practical examples that implement the aforementioned regex functions in PHP.

PHP preg_match

In the first example, the preg_match function is used to simply pattern match the word it-blog in a given URL.

The code below shows the implementation for the above example.

<?php
$my_url = "www.iamcp-us.org";
if (preg_match("/iamcp-us.org/", $my_url))
{
    echo "String $my_url contains iamcp-us.org";
}
else
{
    echo "String $my_url not contain iamcp-us.org";
}
?>

PHP preg_split

Let’s now take a look at another example that uses the preg_split function.

We will take a string phrase and create an array from it using the pattern: element / space.

The text string to use in this example is “I love regular expressions.”

The code below illustrates the implementation of the above example.

<?php

$my_text="Regular expressions";

$my_array  = preg_split("/ /", $my_text);

print_r($my_array );

?>

PHP preg_replace

Let’s now take a look at the preg_replace function, which performs pattern matching and then replaces the pattern with something else.

The code below looks for the word it-blog in the string.

It replaces the word “iamcp-us” with the word “iamcp-us” surrounded by CSS code that highlights the background color.

<?php

$text = "Programming. iamcp-us.org";

$text = preg_replace("/iamcp-us.org/", '<span style="background:yellow">it-blog.ru</span>', $text);

echo $text;

?>

Leave a Reply

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

You may also like...