In the previous article "How does PHP replace a substring of a certain length with a * sign" we introduced a method of string replacement. This time we introduce another method for string replacement and see how this method replaces a given substring with an * sign.
The method introduced above is to perform string replacement by giving the replacement start position and replacement length. This article introduces another method: directly give the substring that needs to be replaced for replacement. Because the replacement substring is directly set, there will be a case problem, which is divided into two situations: case sensitive and Case insensitivity. Today we will introduce the case-sensitive replacement method.
Let’s take a look at the following example:
"; echo str_replace($search2, $replace, $str)."
"; ?>
Observe the above code, we need to change the “hello
” in the string$str
" and "world
" values are replaced with*
; and$str
in the string "hello,world,Hello,World
" There are two types of replacement substrings, the only difference lies in the size of the first letter.
Because the str_replace() function is used to perform string replacement, this function is case-sensitive and case-sensitive, so it only searches for "in the string
$str" hello" and "
world" values, and replace them with
*numbers respectively. So the output result is:
function str_replace()that implements this function.
str_replace($search,$replace,$string,$count)The function can replace some characters in the string in a case-sensitive manner; the function accepts three required parameters
$search(the substring to search for),
$replace(the value to be replaced),
$string(the string), and an optional parameter
$ count(a variable).
$count.
$countneeds to be set to a variable to count and return the number of times replacement is performed. To put it simply, by setting the parameter
$count, you can know how many replacements have been performed in total.
$countthrough code examples.
"; echo "一共执行了 $i"." 次替换
"; echo str_replace($search2, $replace, $str,$i)."
"; echo "一共执行了 $i"." 次替换
"; echo str_replace($search3, $replace, $str,$i)."
"; echo "一共执行了 $i"." 次替换
"; ?>
PHP String Processing (Jade Girl Heart Sutra Edition)", it's free~ come and learn !
The above is the detailed content of How to convert a given substring into * in PHP (case sensitive). For more information, please follow other related articles on the PHP Chinese website!