Home >Backend Development >PHP Problem >How to remove the first two characters of a string in php
php method to remove the first two characters of a string: 1. Use the substr() function, the syntax "substr($str,2)"; 2. Use the substr_replace() function, the syntax "substr_replace($str , '', 0,2)”.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php removes strings The first two characters
Method 1: Use the substr() function
<?php $str = "Hello World!"; echo $str . "<br>"; echo substr($str,2); ?>
Output:
Description:
The substr() function returns a part of the string. The syntax is as follows:
substr(string,start,length)
Description | |
---|---|
string | Required. Specifies a part of the string to be returned.|
start | Required. Specifies where in the string to begin.
|
length | Optional. Specifies the length of the string to be returned. The default is until the end of the string.
|
Method 2: Use substr_replace() function
<?php $str = "World!"; echo $str . "<br>"; echo substr_replace($str, '', 0,2); ?>substr_replace() function is used to replace characters at a specified position and length. , when the replacement value is an empty string, the function of removing the string can be realized. Description: The substr_replace() function replaces part of a string with another string. The syntax is as follows:
substr_replace(string,replacement,start,length)
Description | |
---|---|
Required. Specifies the string to check. | |
Required. Specifies the string to be inserted. | |
Required. Specifies where in the string to begin replacement. | Positive number - starts at the specified position in the string
|
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
|
Recommended learning: "
PHP Video TutorialThe above is the detailed content of How to remove the first two characters of a string in php. For more information, please follow other related articles on the PHP Chinese website!