PHP preg_replace()

PHPz
Release: 2024-08-29 12:49:57
Original
504 people have browsed it

The PHP preg_replace() function is an inbuilt function in the PHP Programming Language. Preg_replace() function helps in performing the regular expressions for searching and replacing the content. Replacing can be done to a single string or multiple string elements that are inside of the array index values. It is similar to the preg_match() up to some extent but in preg_replace() after performing the pattern match on a string, replacing the string element/elements will be done for the specific text. This is one of the important regular expressions in PHP programming language for manipulating string content. You will know about preg_replace() in brief detail below.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

Below is the syntax:

preg_replace( $pattern1, $replacement1, $subject1, $limit1, $count1 )
Copy after login

Parameters

The preg_replace() function usually accepts mainly five parameters as mentioned above. They are $pattern1, $replacement1, $subject1, $limit1 and $count1 parameters.

Know about these parameters with brief description below.

  • $pattern1:$pattern1 parameter of the preg_replace() function contains string element which is helpful in searching the content. It can either be a single string value or an arrays of string/strings.
  • Sreplacement1:$replacement1 parameter of the preg_replace() function is a mandatory parameter. It is helpful to specify the string or a string array with some strings to replace. If this parameter is having a string value then the pattern1 parameter will be an array and all pattern/patterns will be replaced by that specific string. If both the replacement1 and the pattern1 parameters are arrays then every pattern1 will be replaced by the replacement1 counterpart. If the replacement1 is an array and it consists of fewer elements than the pattern1 array then any extra pattern is going to be replaced by the simple empty string.
  • $subject1:$subject1 parameter of the preg_replace() function is a string or an array of string that is used to search and replace the string content from the $pattern variable’s string value.
  • $limit1:$limit parameter of the preg_replace() function is very important and specifies the possible replacements for every pattern at max.
  • $count1:$count parameter of the preg_replace() function is usually an optional parameter. This parameter’s variable’s value-filled with the number which indicates the number of replacements done in the original string.

Return value for the preg_replace() function: The preg_replace() function will return array only if the subect1 parameter is an array/string.

How PHP preg_replace() function work?

Preg_replace() function of PHP programming language works based on the parameters mentioned inside of it. Mainly it works by using the original string which is used when searching and the replacing string/strings. The other parameters may not be that important. Preg_replace() will take regex as it’s a parameter (first parameter) which is used to replace for each match with the 2ndparameter. The second parameter is the plain text whereas the 3rdparameter is the string which is to work. The third parameter can contain $n which is to insert the text matched as a part of the regex rule. If we are writing some complicating multi-part regexes. You can use $0 for using the matched text. Check out the example so that you will know in example 2 below.

Examples to Implement preg_replace() function in PHP

Below are some examples mentioned:

Example #1

In the below example, a variable “$copy_date1” is created with the value “Copyright 1998” and then the same variable is created with the preg_replace() function as a value. So the result of the preg_replace() function will be stored in the “$copy_date1” variable. Inside preg_replace() function, the values 0-9 (all) will be replaced by the string value “2020” and stored in the “$copy_date1” variable. Then the value present in the “$copy_date1” will be printed which is the changed string. Then “Copyright 2020” will be printed in the output.

Code:

Copy after login

Output:

PHP preg_replace()

Example #2

In the below example, a variable “$a1” is created with the string value “Fool bool tool fool”. Then again the same naming variable is created with the preg_replace() function. In the preg_replace() string value is used to search which are ending with a cool after any alphabet. If there is anything then those string values will be printed one by one after the “Got word:” word. And this result will be stored in the “$a1” variable. Then the word will be printed using the print function.

Code:

Copy after login

Output:

PHP preg_replace()

Example #3

In the below example, the date will be changed in the string value as mentioned in the replacement1 term. At first a variable “$date1” is created inside of the PHP tags with the value “Sep 29, 2020”. Then the “$pattern1” variable is created with meta-characters that are used to find a word in the character. Then the “$replacement1” variable is created with the value ”${1} 6, $3”. Here “1” inside flower braces will remain the same and then in the 2ndvalue 6 will be replaced in the place of 29 and $3 represents the 3 string elements inside of the array. If “$1 ” is placed then only Sep will be printed if the $2 is represented then replaced value only 6 will be printed. So the $3 is mentioned to mention all the 3 elements which are to be printed using echo function.

Code:

Copy after login

Output:

PHP preg_replace()

Example #4

In the below example, multiple string elements are replaced using the array function with the single string values in the index values to change. At first “string1” variable is created with a string sentence with many words. Then “$patterns1” variable is created with array() function as variable. Then inside of the $patterns[index value] is/are stored with a different string that is already present in the original string. Similarly the same is done for the “$replacements1” variable with different string values to change the list of strings inside of the string sentence. Then “$newstr1” variable is created with the value as preg_replace() function. Then echo function is used to print the changed string sentence. Check out this in the output image.

Code:

The String after the replacements: " .$newstr1; ?>
Copy after login

Output:

PHP preg_replace()

Conclusion

I hope you understood what is the definition of PHP preg_replace() function with its syntax along with various parameter explanation, How preg_replace() function works along with the various examples to implement preg_replace() function in PHP Programming language.

The above is the detailed content of PHP preg_replace(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!