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

5 essential commands to interact with an online git repository: push, pull, clone, fetch, remote (video)
Nov 27, 2022 programming

5 essential commands to interact with an online git repository: push, pull, clone, fetch, remote (video)

5 commands to interact with and online git repository ...

13 Min Read Read More
PHP calculate distance between 2 places in 9 easy steps
Feb 14, 2023 programming

PHP calculate distance between 2 places in 9 easy steps

Today we are going to talk about calculating the distance between two place in PHP in a few easy steps. ...

7 Min Read Read More
PHP Array Flip
Feb 14, 2023 programming

PHP Array Flip

Today we are going to talk about array flip function in PHP. Array flip is very simple yet useful array function that helps you flip and array. ...

4 Min Read Read More
Top Skills for Developers: Non-Technical and Technical Essentials
Apr 05, 2023 programming

Top Skills for Developers: Non-Technical and Technical Essentials

Being a successful developer requires a combination of both technical and non-technical skills. ...

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