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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor

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.
