Get the App
SLTechnology News&Howtos  ›  Development  › 

How to remove the first two characters of a string by php

Shulou Source: shulou.com Published: 2022-06-01 18:26:03 10月04日 Update

This article is a detailed introduction to "how to remove the first two characters of a string in php". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "how to remove the first two characters of a string in php" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of Xiaobian.

php Methods to remove the first two characters of a string: 1. Use the substr() function, syntax "substr($str,2)";2. Use the substr_replace() function, syntax "substr_replace($str,'',0,2)".

Operating environment of this tutorial: Windows 7 system, PHP7.1 version, DELL G3 computer

Php removes the first two characters of a string

Method 1: Use the substr() function

Output:

Description:

The substr() function returns a portion of a string. The syntax is as follows:

substr(string,start,length) Parameter Description string Required. Specifies a string of which part is to be returned. Start is required. Specifies where the string starts.

Positive number-starts at the specified position in the string

Negative-starts at the specified position from the end of the string

0 -starts at the first character in the string

Length is optional. Specifies the length of the string to return. The default is until the end of the string.

Positive-returns from where the start parameter is located

Negative number-returns from end of string

Note: If the start argument is negative and length is less than or equal to start, length is 0.

Method 2: Use the substr_replace() function

substr_replace() function is used to replace the specified position, the specified length of the character, when the replacement value is empty string Yes can be achieved to remove the function of the string.

Description:

The substr_replace() function replaces part of a string with another string. The syntax is as follows:

substr_replace(string,replacement,start,length) Parameters Description string Required. Specify the string to check. Replacement is required. Specifies the string to insert. Start is required. Specifies where the substitution starts in the string.

Positive number-starts at the specified position in the string

Negative-starts at the specified position from the end of the string

0 -starts at the first character in the string

Length is optional. Specify how many characters to replace. The default is the same length as the string.

Positive number-length of string being replaced

Negative-Number of characters replaced starting at the end of the string

0 -insert rather than replace

Note: If the start parameter is negative and length is less than or equal to start, length is 0.

Read here, this article "php how to remove the first two characters of the string" article has been introduced, want to master the knowledge points of this article also need to be used by yourself to understand, if you want to know more about the relevant content of the article, welcome to pay attention to the industry information channel.

Tags: Character string function position negative number parameter positive number grammar length article method content end comment proper same function idea place tutorial Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker OPPO Reno Shulou Information Shulou Tech Info