BLACK FRIDAY! 50% OFF!     |        Get Skills for Your IT Future!     | 

4d 06h
close
Cart icon
User menu icon
User icon
Lightbulb icon
How it works?
FAQ icon
FAQ
Contact icon
Contact
Terms of service icon
Terms of service
Privacy policy icon
Privacy Policy

Regular Expressions (RegEx) in PHP: A Powerful Tool for Text Processing

Regular expressions (RegEx) are a powerful tool that allows you to search, match, and manipulate text efficiently. In the context of PHP programming, regular expressions are particularly useful for:

  • Processing input data
  • Validating forms
  • Searching for patterns within text

In this article, we'll explore the basics of regular expressions in PHP and show you how to build a simple regular expression.

Working with Regular Expressions in PHP

In PHP, regular expressions are most commonly handled using functions from the PCRE (Perl-Compatible Regular Expressions) library. These functions start with the prefix preg_.

Key PCRE Functions in PHP

Here are some of the basic PCRE functions you'll use in PHP:

  • preg_match(): Checks if a pattern matches a given text.
  • preg_replace(): Replaces occurrences of a pattern in the text with a new string.
  • preg_split(): Splits the text based on a pattern.

Using preg_match()

The preg_match() function is used to verify whether a given pattern exists in a text string.

<?php

$text = "Today the weather is beautiful.";

$pattern = '/beautiful/';


if (preg_match($pattern, $text)) {

    echo "The word 'beautiful' was found in the text!";

} else {

    echo "The word 'beautiful' was not found in the text.";

}

?>

Output:

The word 'beautiful' was found in the text!

In this code, the script checks whether the word beautiful (defined by $pattern) appears in the text string $text. It then outputs an appropriate message.

Using preg_replace()

The preg_replace() function allows you to replace occurrences of a pattern in a string with a new value.

<?php

$text = "Apples are tasty, and pears are delicious.";

$pattern = '/apples/i';

$replacement = 'Fruits';


$new_text = preg_replace($pattern, $replacement, $text);

echo $new_text;

?>

Output:

Fruits are tasty, and pears are delicious.

Here, the word apples is replaced with Fruits, demonstrating how preg_replace() can modify the text.

Using preg_split()

The preg_split() function allows you to split a text string based on a regular expression pattern.

<?php

$text = "cat, dog; fish|bird. rabbit";

$pattern = '/[;,|.]/';


$animals = preg_split($pattern, $text);

print_r($animals);

?>

Output:

Array

(

    [0] => cat

    [1] => dog

    [2] => fish

    [3] => bird

    [4] => rabbit

)

In this example, the code splits the string by commas, semicolons, pipes, and periods, resulting in an array of animal names.

Modifying the Pattern

Take note of how we specify the delimiters for splitting the string in the pattern using square brackets ($pattern). Let's see what happens if we leave out the dot (.) and comma (,):

<?php

$text = "cat, dog; fish|bird. rabbit";

$pattern = '/[,.]/';


$animals = preg_split($pattern, $text);

print_r($animals);

?>

Output:

Array

(

    [0] => cat

    [1] => dog; fish|bird

    [2] => rabbit

)

Now, the string is split only by commas and periods, so semicolons and pipes are left intact.

Conclusion

Regular expressions are a powerful tool for text manipulation that can significantly simplify text processing in PHP. Whether you're validating form input, searching for patterns, or splitting strings, understanding and using regular expressions will enhance your ability to work with text efficiently in PHP.