How to implement character replacement in php

藏色散人
Release: 2023-02-28 18:48:01
Original
2742 people have browsed it

How to implement character replacement in php

php implementation method of replacing characters

php replaces characters in a string. There are usually two functions that can Use, str_replace() and strtr()

str_replace() functionDefinition and usage

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

该函数必须遵循下列规则:
如果搜索的字符串是数组,那么它将返回数组。
如果搜索的字符串是数组,那么它将对数组中的每个元素进行查找和替换。
如果同时需要对数组进行查找和替换,并且需要执行替换的元素少于查找到的元素的数量,那么多余元素将用空字符串进行替换
如果查找的是数组,而替换的是字符串,那么替代字符串将对所有查找到的值起作用。
Copy after login

Note: This function is case-sensitive. Please use the str_ireplace() function to perform a case-insensitive search.

Note: This function is binary safe.

Syntax

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

Parameters

find必需。规定要查找的值。
replace必需。规定替换 find 中的值的值。
string必需。规定被搜索的字符串。
count可选。对替换数进行计数的变量。
Copy after login

strtr() function definition and usage

strtr() function converts specific strings character of.

Note: If the lengths of the from and to parameters are different, they will be formatted to the shortest length.

Grammar

strtr(string,from,to)
Copy after login

Or:

strtr(string,array)
Copy after login

Parameters

string必需。规定要转换的字符串。
from必需(除非使用数组)。规定要改变的字符。
to必需(除非使用数组)。规定要改变为的字符。
array必需(除非使用 from 和 to)。数组,其中的键名是更改的原始字符,键值是更改的目标字符。
Copy after login

For more related knowledge, please pay attention to PHP Chinese website! !

The above is the detailed content of How to implement character replacement in php. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!