Home  >  Article  >  Backend Development  >  How to remove a certain character from a php string

How to remove a certain character from a php string

青灯夜游
青灯夜游Original
2021-03-22 18:21:263710browse

In PHP, you can use the str_replace() function to remove a certain character in a string. str_replace() is used to replace some characters in a string in a case-sensitive manner. The syntax format "str_replace('To remove characters','',string)".

How to remove a certain character from a php string

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php remove a certain character from the string

In PHP, you can use the str_replace() function to remove a certain character in a string.

Example: Remove the a character from the string

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);
$str="abcdefg";
echo "原字符串:",$str,"<br>";
$str=str_replace(&#39;a&#39;,&#39;&#39;,$str);
echo "去掉a字符的字符串:",$str;
?>

Output:

原字符串:abcdefg
去掉a字符的字符串:bcdefg

Related function description:

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

This function must follow the following rules:

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

  • If the searched string is an array, then it will find and replace each element in the array.

  • If an array needs to be searched and replaced at the same time, and the elements to be replaced are less than the number of found elements, the excess elements will be replaced with empty strings .

  • If you search an array and replace only one string, the replacement string will work for all found values.

Grammar

str_replace(find,replace,string,count)

How to remove a certain character from a php string

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove a certain character from a php string. 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