Common character string functions in php (3) Replace str_replace () with substrings and phpstr_replace
MixedStr_replace(Mixed$search
, Mixed$replace
, Mixed$subject
[, Int&$count
]);
This function returns a string or array. This string or array issubject
Allsearch
Arereplace
Result After replacement.
Both the return value and the first three parameters can be strings or arrays. The fourth parameter specifies the number of replicas for a variable.
Example 1: All parameters are strings.
$bodytag = str_replace ( "%body%" , "black" , "<body text='%body%'>" );
=> "<Body text = 'black'>"
Example 2: parameter 1 is an array and 23 is a string.
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U", "P", "h");$onlyconsonants = str_replace($vowels, "@", "Hello world of php", $count);
=> "H @ ll @ w @ rld @ f p @ p" // The p in this example is not matched by P. This function isCase Sensitive. Use the str_ireplace () function to ignore case sensitivity.
=> $ Count => 5 // 5 times. E, o, h
Example 3:
$str = "Line 1\nLine 2\rLine 3\r\nLine 4\n";$order = array("\r\n", "\n", "\r");$replace = '<br/>';$newStr = str_replace($order, $replace, $str, $count);
=> $ Count => 4 // four times. \ N, \ r, \ r \ n, \ n, respectively. Note: Because \ r \ n is written in $ order, the Third matching is not divided into two \ r, \ n.
Example 4:
$letters = array( 'a' , 'p' );$fruit = array( 'apple' , 'pear' );$text = 'a p' ;$output = str_replace ( $letters , $fruit , $text , $count);echo $output ;echo $count;
=> Guess what the output will be. Example 4 can be done. This function is enough.