Home > Article > Backend Development > Detailed explanation of str_replace() in php (with code examples)
Strings are a commonly used type in php. Replacement of strings is not as convenient as arrays. So how to replace characters in a string? This article will explain Let’s take a look at how to use the str_repalce()
function. First, let’s take a look at the syntax of the function:
str_replace ( mixed $search , mixed $replace , mixed $subject , int $count = ? )
$search: search Target value, multiple targets can be specified.
#$replace: the replacement value of search.
$subject: Array or string to perform replacement
$count: Optional, if specified, its value will be set is the number of times replacement occurs.
Return value: This function returns the replaced array or string.
Code example:
1. There are three required parameters
<?php $str="ok is great,that is php.cn"; $str1=str_replace("is","hehe",$str); print_r($str1); ?>
输出:ok hehe great,that hehe php.cn
2. There are four parameters
<?php $str="ok is great,that is php.cn"; $str1=str_replace("is","hehe",$str,$count); print_r($str1); echo "<br>"."总共替换了:".$count; ?>
输出:ok hehe great,that hehe php.cn 总共替换了:2
Recommended: 《2021 PHP Summary of interview questions (collection)》《php video tutorial》
The above is the detailed content of Detailed explanation of str_replace() in php (with code examples). For more information, please follow other related articles on the PHP Chinese website!