How toTech

How to use PHP preg_replace function?

Use PHP preg_replace function: If you want to perform an expression for search and want to replace the content you can use the preg_replace function. Generally, regular expressions are powerful pattern matching algorithms that can be performed in a single expression. These expressions use arithmetic operations to create complex expressions. In this article we will get to know how to use PHP preg_replace function.

The reason why we use regular expressions in PHP is that they simplify identifying patterns in string data by calling a single function. They also highlight keywords in search results. Regular expressions can be used to identify the template tags and replace them with actual data. PHP has built-in functions that allow us to work with regular functions. Those functions are,
a) preg_match
b) preg_split
c) preg_replace
Let us go through the preg_replace function in this article.

PHP preg_replace function

As mentioned above, the preg_replace function is an inbuilt function in PHP. It performs a regular expression for search and replaces the content. Let us go through how to use the preg_replace function in PHP through relevant examples.

Syntax

The syntax of the preg_replace function is stated below,

preg_replace( $pattern, $replacement, $subject, $limit, $count ) 

Parameters

This function accepts five parameters. Refer below to know the detailed explanation of these parameters one by one .

a) $pattern: This parameter contains the string element which is used to search the content which can be any string or an array of string.

b) $replacement: This is also a mandatory parameter which specifies the string or an array with relevant strings to replace.

c) $subject: The string or an array with strings to search and replace.

d) $limit: This parameter is used to specify the maximum possible replacements for each pattern.

e) $count: This is an optional parameter where it gets filled by the number of replacements done.

Also learn how to create a database in PHP using MySQL by exploring the highlighted link below.
How to create a database in PHP using MySQL

Return value

This function returns an array if the subject parameter is an array or a string otherwise.

Examples: Below are some relevant examples in regard to the above function.

Program 1: A program to illustrate preg replace function.

<?php
// PHP program to illustrate 
// preg_replace function

$string= 'January 5, 2018';
$pattern= '/(\w+) (\d+), (\d+)/i';
$replacement= '${1} 10, $3';

// print output of function
echopreg_replace($pattern, $replacement, $string);
?>

Output: The output of the above program is as shown below,

January 10, 2018

Program 2: A program to illustrate the preg function

<?php 
// PHP program to illustrate 
// preg_replace function
$subject= array('1', 'GFG', '2',
'Geeks', '3', 'GCET', 'Contribute', '4'); 
$pattern= array('/\d/', '/[a-z]/', '/[1a]/'); 
$replace= array('X:$0', 'Y:$0', 'Z:$0'); 

// Print Result return by function
echo"preg_replace returns\n";
print_r(preg_replace($pattern, $replace, $subject)); 
?>

The output of the above code is depicted as below,

preg_replace returns 
Array
(
[0] => X:Z:1
[1] => GFG
[2] => X:2
[3] => GY:eY:eY:kY:s
[4] => X:3
[5] => GCET
[6] => CY:oY:nY:tY:rY:iY:bY:uY:tY:e
[7] => X:4
)

Program 3: A program to illustrate the preg function

<?php    
$copy_date = "Copyright 1999";
$copy_date = preg_replace("([0-9]+)", "2000", $copy_date);

print $copy_date;
?>

The output of the above code is as shown below,

Copyright 2000 

If you want to know how you can send an email in PHP, you can explore the highlighted link below.
How to send an email in PHP

FAQ’s

Here are some basic FAQ’s which can clear up your mind in regard to this article. Scroll down below to know more,

What is preg_match?

Regular expressions in PHP. PHP has built-in functions that allow us to work with regular functions. Let’s now look at the commonly used regular expression functions in PHP. preg_match – this function is used to perform a pattern match on a string. It returns true if a match is found and false if a match is not found.

What are PHP expressions?

In PHP, almost anything you write is an expression. The simplest yet most accurate way to define an expression is “anything that has a value”. Functions are expressions with the value of their return value.

What is Crypt() in PHP?

The crypt() function returns a hashed string using DES, Blowfish, or MD5 algorithms. This function behaves differently on different operating systems. There are some constants that are used together with the crypt() function. The value of these constants is set by PHP when it is installed.

To conclude: Hence, through this article we have learnt how to use PHP preg_replace function using relevant examples. In case if you have any queries in regard to this article you can comment them below in the comment section. Also, do share the content if you like it. For some more interesting tech articles and tech news visit Morphigo.com and delve into amazing content on web.

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Related Articles

Leave a Reply

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

Back to top button
Close