preg_replace() function in php
PHP preg_replace() function is used to search a particular content in a string and replace it with another content with the help of a regular expression. It is an inbuilt function of PHP.
The return type of this function depends on the input parameter given for replacement. If the input is an array then this function returns an array and in case of string it returns a string.
- $regular_expression represents the code that we used to search the content. It can be either an array or a string. It is required.
- $replacement_content represents the code that replaces to string. it can also be either an array or a string. It is also a mandatory parameter.
- $sub: this is the code with the strings to search and replace. It can also be either an array or a string.
- $limit: it is a numeric value that represents the maximum number of possible replacements for each regular expression or pattern.
- $count: it is also a numeric value that represents the number of replacements done by the function. It is an optional parameter.
<?php $pattern = '/aa/'; $replacement_content='a'; $sub='aabaabaadaafaagaahaaiaajaak'; $result = preg_replace($pattern,$replacement_content,$sub); // it will replace all double a with single a echo "Old string is : $sub <br>"; echo "After replacement new string is : $result"; ?>
Waiting for your comments