Free cookie consent management tool by TermsFeed Generator PHP Regular Expressions | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Regular Expressions

PHP Regular Expressions

Last Updated on Mar 22, 2023

What is Regular Expression?

A regular expression is a sequence of characters that specifies a search pattern. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.

You can read more at Wikipedia

The patterns are general and are not specific to php. You can learn and practice regex at regex101

Regular Expression in PHP

In php your regular expression should go between "//" so any pattern you have, would be something like this:

"/pattern/"

Check Patterns

Now let’s see how we can check that pattern against a string in php.

There are many functions but we are going to talk about 2 functions that are so useful and powerful

  • preg_match
  • preg_replace

Preg Match

preg_match(pattern, string,matches)

pattern: the regex pattern

string: where to search

matches: a variable you specify so if there were any matches those matches will be saved as an array in that variable.

Returns 1 if it finds the pattern and 0 if it dosn’t

$string = "Hello world! Hello Pratham! Hello Friends!";
$pattern = "/world/";

echo preg_match($pattern,$string,$matches);
// returns 1 because world exist

print_r($matches);
/* output is
Array
(
    [0] => world
)
*/

Preg Replace

preg_replace(pattern, replacements,input)

It’s like saying find all the matches and replace them

pattern: the regex pattern

replacements: can be a string or an array

input: can be a string or an array of strings

returns a string or an array of strings where the matched patterns have been replaced with the replacements

$string = "Hello world! Hello Pratham! Hello Friends!";
$pattern = "/Hello/";

$newString = preg_replace($pattern,"Goodbye",$string);
echo $newString;
/* output is
Goodbye world! Goodbye Pratham! Goodbye Friends!
*/

https://youtu.be/ZMACwe8S5Ow

Conclusion

Now you know about regular expressions in PHP.

I recommend you to open a PHP files and use both of the functions we learned to apply regex. see the result.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • regular expressions in PHP
  • preg replace
  • preg match

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP curl
Mar 22, 2023 programming

PHP curl

Today we are going to talk about curl in PHP. Sometimes we need to send requests to another url to get data, maybe another API. So how can we do that in PHP? curl is the answer. ...

6 Min Read Read More
PHP Array Intersect
Feb 14, 2023 programming

PHP Array Intersect

Today we are going to talk about array intersect functions in PHP .If you want to find the items in your array that are present in the other arrays, array intersect is your friend. ...

5 Min Read Read More
Creating a Simple Jupyter Notebook in PHP
Aug 28, 2024 programming

Creating a Simple Jupyter Notebook in PHP

This tutorial will guide you through the steps to create a simple PHP-based notebook interface. The notebook allows you to write and run PHP code in a web browser, maintaining the state between code executions. ...

28 Min Read Read More
React Beginner to Advance 2025: A Complete Guide to Building Modern Web Applications
Jul 18, 2025 programming

React Beginner to Advance 2025: A Complete Guide to Building Modern Web Applications

React JS has firmly established itself as one of the most widely adopted JavaScript libraries for building dynamic user interfaces. Whether you're crafting a small personal website or architecting a large-scale enterprise dashboard, React's component-based architecture and flexible ecosystem allow developers to create highly performant, scalable, and maintainable applications. ...

29 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...