Here, you will learn how to replace the text from a string. In simple words, you can say that you need to replace a portion of a string with new text.
Simple search – and – replace functions like str_replace() are useful for replacing literal strings. However, if you need to replace more complex patterns of text, you can use PHP’s regular expression string replacement functions, preg_replace() and preg_replace_callback() . Replacing Text with preg_replace() The preg_replace() Function is used to match a pattern against a target string, much like preg_match(), and replace the matched text with different text. In its most basic form, preg_replace() takes three arguments:
The preg_replace() returns the target string with any matched text replaced by the replacement text. $text = “The wholesale price is $90.20.”; You can also use backreferences within the replacement string — simply write a dollar ( $ ) symbol followed by the backreference number: $text = “Author: Steinbeck, John”; If you want to include the entire matched text in the replacement string, use $0 (a dollar followed by zero): $text = “T-shirt: $3.99”; You can also pass an array of target strings for preg_replace() to work on, much like using preg_grep(). If you do this, preg_replace() returns the array of strings with any matched text replaced by the replacement text: Code $text = array( Output This code displays: You can also pass an array of regular expression strings to the function, and it will match and replace each expression in turn with the replacement string: Code $text = “The wholesale price is $89.50. “ . Output The wholesale price is [WELLDONE]. The product will be released on [WELLDONE]. If you also pass an array of replacement strings, the matched text from each expression in the expressions array is replaced by the corresponding string in the replacements array: Code $text = “The wholesale price is $89.50. “ Output The wholesale price is [PRICE FIXED]. The product will be released on [DATE FIXED]. If your replacements array contains fewer elements than your expressions array, matched text for any expression without a corresponding replacement is replaced with an empty string. Code $text = “The wholesale price is $89.50. “ . Output The wholesale price is [PRICE CENSORED]. The product will be released on. preg_replace() supports two more optional arguments.
This pattern replaces a percentage figure (followed optionally by commas and spaces) with an empty string. Because a limit argument of 2 was supplied, only the first two matches are replaced.The second optional argument is a variable to hold the number of replacements performed. (If you want to use this argument but you don ’ t want to limit the number of replacements, pass – 1 for the previous argument.) This example replaces the character ‘ % ’ with the string “ percent ” four times, and displays the number of replacements: preg_replace( “/\%/”, “ percent”, “14%, 59%, 71%, 83%”, -1, $count ); The number stored in $count is the total number of replacements performed. So if you pass an array of 10 target strings and text is replaced once in five of them, then $count equals 5. |