Home > Article > Backend Development > How to convert a given substring into * in PHP (case sensitive)
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:
<?php $str = 'hello,world,Hello,World'; $replace = '*'; $search1 = 'hello'; $search2 = 'world'; echo str_replace($search1, $replace, $str)."<br>"; echo str_replace($search2, $replace, $str)."<br>"; ?>
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.
$count needs 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.
$count through code examples.
<?php header("Content-Type: text/html;charset=utf-8"); //设置字符编码 $str = 'hello,world,Hello,world'; $replace = '*'; $search1 = 'hello'; $search2 = 'world'; $search3 = ','; echo str_replace($search1, $replace, $str,$i)."<br>"; echo "一共执行了 $i"." 次替换<br><br>"; echo str_replace($search2, $replace, $str,$i)."<br>"; echo "一共执行了 $i"." 次替换<br><br>"; echo str_replace($search3, $replace, $str,$i)."<br>"; echo "一共执行了 $i"." 次替换<br>"; ?>Output result: Okay, that’s all. If you want to know anything else, you can click this. → → Finally, I recommend reading a classic course "
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!