Detailed introduction to the method of replacing specified characters in PHP (case sensitive)

醉折花枝作酒筹
Release: 2023-03-11 21:48:02
Original
2634 people have browsed it

In the previous article, we learned about the method of replacing specified characters in a string. If you need it, please read "How to replace specified characters in php in one step (case insensitive)". This time we introduce to you another method of replacing specified characters in a string. You can refer to it if you need it.

In our previous article, we introduced the method of using the str_ireplace function to replace specified characters in a string. This method is not sensitive to the case of the characters being replaced. This time we will introduce a way to replace the characters being replaced. Replace character-sensitive functions.

First let’s look at a small example.

<?php
echo str_replace("abyss","dragon","When you stare at the abyss, the Abyss is staring at you.");
?>
Copy after login

Let’s take a look at the result of this example

When you stare at the dragon, the Abyss is staring at you.
Copy after login

Let’s look at this example. In the string that needs to be replaced, we can find that there are two abyss, but the first letter of one is capitalized. The first letter is not capitalized, but the

result is not the same as the str_ireplace function introduced in the previous article. As long as the letters are the same, they can be replaced. When using this function, you must ensure that the letters are the same and the size is the same. The same goes for writing.

Let’s take a look at this function in detail.

str_replace() function replaces some characters in a string (case sensitive).

Then let’s take a look at the syntax of this function.

str_replace(find,replace,string,count)
Copy after login

The syntax of this function is exactly the same as the syntax of the str_ireplace function in the previous article. The first parameter specifies the value to be found; the second parameter specifies the replacement of the found value; the third parameter is the string that needs to be replaced; the fourth parameter also counts the number of replacements. a variable.

But using this function is also conditional and must follow the following rules:

  • If the searched string is an array, it will return an array.

  • If the search string is an array, it will find and replace every element in the array.

  • If an array is searched but only one string is replaced, the replacement string will be used for all found values.

That’s all. If you want to know anything else, you can click this. → →php video tutorial

The above is the detailed content of Detailed introduction to the method of replacing specified characters in PHP (case sensitive). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template