PHP preg_replace() Function

PHP preg_replace_callback() Function
PHP preg_last_error() Function

In this article, you will learn how to find and replace string in PHP. The preg_replace() function in PHP returns a string or array of strings where all matches of a pattern or list of patterns found in the input are replaced with substrings.

what is the syntax of the PREG_REPLACE function in php?

preg_replace(patterns, replacements, input, limit, count)
ParameterDescription
patternsRequired. Contains a regular expression or array of regular expressions
replacementsRequired. A replacement string or an array of replacement strings
inputRequired. The string or array of strings in which replacements are being performed
limitOptional. Defaults to -1, meaning unlimited. Sets a limit to how many replacements can be done in each string
countOptional. After the function has executed, this variable will contain a number indicating how many replacements were performed
PHP preg_replace() method

examples of the PREG_REPLACE function

Example 1. In this example, we use a case-insensitive regular expression to replace Microsoft with W3Schools in a string.

<?php
$str = 'Visit Microsoft!';
$pattern = '/microsoft/i';
echo preg_replace($pattern, 'W3Schools', $str);
?>
PHP preg_replace_callback() Function
PHP preg_last_error() Function
en English
X
Scroll to Top