Home  >  Article  >  Backend Development  >  How to replace specified characters in PHP?

How to replace specified characters in PHP?

Guanhui
GuanhuiOriginal
2020-06-24 09:40:403589browse

How to replace specified characters in PHP?

How to replace specified characters in PHP?

In PHP, you can use the "str_replace()" function to replace specified characters. The function of this function is to replace substrings. Its usage is "str_replace($search,$replace,$subject)" ”, the return value is the replaced string.

Recommended video tutorial: "PHP Programming from Beginner to Master (Learning Route)"

str_replace() Description

str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) : mixed

This function returns a string or array. This string or array is the result of replacing all search in subject with replace.

If there are no special replacement requirements (such as regular expressions), you should use this function to replace ereg_replace() and preg_replace().

str_replace() Parameters

If search and replace are arrays, then str_replace() will perform mapping replacement on subject. If the number of replace values ​​is less than the number of search values, additional replacements will be performed using empty strings. If search is an array and replace is a string, then the replacement of each element in search will always use this string. This conversion does not change case.

If search and replace are both arrays, their values ​​will be processed in sequence.

  • search

The target value to search for, which is needle. An array can specify multiple targets.

  • replace

    Replacement value for search. An array can be used to specify multiple replacements.

  • subject

    Array or string to perform replacement. That is haystack.

    If subject is an array, the replacement operation will traverse the entire subject, and the return value will also be an array.

  • count

    If specified, its value will be set to the number of times the replacement occurred.

Recommended tutorial: "PHP Tutorial"

The above is the detailed content of How to replace specified characters in PHP?. 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