Home >Backend Development >PHP Problem >How to convert a given substring into * in PHP (case sensitive)

How to convert a given substring into * in PHP (case sensitive)

青灯夜游
青灯夜游Original
2021-08-09 19:30:041732browse

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 = &#39;hello,world,Hello,World&#39;;
$replace = &#39;*&#39;;
$search1 = &#39;hello&#39;;
$search2 = &#39;world&#39;;
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:

How to convert a given substring into * in PHP (case sensitive)

#Understand how to replace the given substring with an * sign in a case-sensitive manner. Let's take a look at the

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).

Through the above example, we know the meaning of the first three required parameters. Let’s talk about the omitted parameters

$count.

The value of parameter

$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.

Let’s take a look at the usage of parameter

$count through code examples.

<?php
header("Content-Type: text/html;charset=utf-8");    //设置字符编码
$str = &#39;hello,world,Hello,world&#39;;
$replace = &#39;*&#39;;
$search1 = &#39;hello&#39;;
$search2 = &#39;world&#39;;
$search3 = &#39;,&#39;;
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:

How to convert a given substring into * in PHP (case sensitive)

Okay, that’s all. If you want to know anything else, you can click this. → →

php video tutorial

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn