String replacement.
Syntax: string str_replace (string needle, string str, string haystack );
Return Value: String
Function Type: Data Processing
Description
This function substitutes str into the haystack string and replaces all needle with str. Mlevine@adtraq.com (11-Apr-1999) pointed out that in PHP 3.0.7, this function has some bugs, and the nadeem@bleh.org (05-Jun-1999) supplement in PHP 3.0.8 version function will return to normal.
Example
Replace % body % with black in the following example
<Php
$ Bodytag = str_replace ("% body %", "black", "<body text = % body %> ");
Echo $ bodytag;
>
Format:
[@ Str_replace ("old content to be replaced", "new character to replace the original content", $ variable name of the content to be replaced)]
[@ Str_replace (array ('old 1', 'old 2', 'old 3'), array ('new 1', 'new 2', 'new 3 '), $ variable name of the content to be replaced)]
[@ Str_replace (array ('old 1', 'old 2', 'old 3'), 'new content', $ variable name of the content to be replaced)]
Instance:
Replace multiple-to-one: to clear all <p> </p> labels in the content field, replace them with null.
[@ Str_replace (array ('<p>', '</p>'), '', $ Content)]
One-to-one replacement: replace all <br> tags in the content field with <p>
[@ Str_replace ('<br>', '<p>', $ Content)]
Replace multiple-to-multiple: replace <br> with <br/> In the content field, <p> change
Summary of str_replace function usage in PHP
Php Regular Expression Function preg_replace () parameter description