Home  >  Article  >  Backend Development  >  How to replace string variables in php

How to replace string variables in php

藏色散人
藏色散人Original
2021-03-03 09:33:292839browse

How to replace string variables in php: 1. Replace part of the string with another string through the substr_replace function; 2. Use a string to replace other characters in the string through the str_replace function.

How to replace string variables in php

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

In the work of the past few days, the prototype of the "Bank Card" page is as follows. Among them, only the last four digits of the card numbers of different bank cards can be displayed, and other numbers are hidden with * characters.

After communicating with the front-end, I will hide the numbers. This uses the string replacement substr_replace function in PHP.

How to replace string variables in php

#PHP string replacement, as the name suggests, is used to replace a specified string from a string.

Related functions are as follows:

 substr_replace()——Replace part of a string with another string

 str_replace() ——Use a string to replace other characters in the string

1. substr_replace()

Syntax:

substr_replace(string,replacement,start,length)
##ParametersDescription## at the first character in the string #lengthReturn value
string Required. Specifies the string to check.
replacement Required. Specifies the string to be inserted.
start

Required. Specifies where in the string to begin replacement.

  • Positive numbers - start replacing at the specified position in the string
  • Negative numbers - start at the specified position from the end of the string Replace
  • 0 - Start replacing
Optional. Specifies how many characters to replace. The default is the same as the string length.

    Positive number - the length of the string to be replaced
  • Negative number - indicates the distance from the end of the substring to be replaced
  • string Number of characters at the end.
  • 0 - insert instead of replace
Return the replaced string


##2. str_replace( )

Syntax:

str_replace(find,replac,string,count)
##ParametersDescription findreplace
Required. Specifies the value to look for.
##Required. Specifies the value to replace the value in find. string
Required. Specifies the string to be searched for. count
Optional. Variable counting the number of substitutions. Return value
Return a string or array with replacement value

The above are the basic knowledge points of string replacement substr_replace() and str_replace() functions, so in actual work, I used the first one-substr_replace() function.

[Recommended: PHP Video Tutorial]

After inquiry, I learned that the current domestic bank cards are 16 or 19 digits in length, so I checked from the data table After getting the bank card number, first use the strlen() function to count the length of the string.

When the bank card is 16 digits, the first 12 digits are replaced by 12 *, that is, the replacement is ************;

When the bank card When it is a 19-digit number, the first 15 digits are replaced by 15 *, that is, the replacement is ****************;

The code is as follows:

How to replace string variables in php

The test results are as follows:

How to replace string variables in php


The above is the detailed content of How to replace string variables 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