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

the ultimate guide to Git: learn everything you need in less than 1 hour
Nov 28, 2022 programming

the ultimate guide to Git: learn everything you need in less than 1 hour

Here is everything you need to know about git in a series of short question and answers. ...

11 Min Read Read More
PHP DataTypes
Mar 21, 2023 programming

PHP DataTypes

Today we are going to learn about Data Types in PHP. Data types are another important subject in programming. The better you know the data types you are working with, the more skillful you become. ...

5 Min Read Read More
PHP Interacting with Python
Mar 22, 2023 programming

PHP Interacting with Python

Today we are going to talk about interacting with a python file in PHP. Both PHP and Python are very powerful languages. you can run a python program from your PHP code. ...

6 Min Read Read More
PHP Callback Functions
Jan 08, 2023 programming

PHP Callback Functions

Today we are going to talk about callback functions in PHP. sometimes we need to pass a function as an argument of another function, that's exactly what callback function is. ...

7 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 ...