search
HomeBackend DevelopmentPHP ProblemHow 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 = &#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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.