Home > Article > Backend Development > How to remove the first 4 characters from a php string
3 methods: 1. Use "ltrim(string, "substring composed of the first 4 characters")"; 2. Use "substr_replace(string,"",0,4)", The first 4 characters can be replaced with null characters; 3. Use "mb_substr(string, 4)" to intercept the string starting from the 5th character and return it.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php string migration 3 ways to remove the first 4 characters
##Method 1: Use the ltrim() function
ltrim() function can remove strings Whitespace characters or other predefined characters on the left. The syntax is as follows:rtrim(字符串,预定义字符)Just set the predefined characters to a substring of the first 4 characters.
<?php $str = "Hello World!"; echo $str . "<br>"; echo ltrim($str,"Hell"); ?>
Method 2: Use the substr_replace() function
substr_replace() function to replace part of the string with another character string. Just replace the first 4 characters with null characters.<?php $str = "123456789"; echo $str . "<br>"; echo substr_replace($str,"",0,4); ?>
Method 3: Use the mb_substr() function
The mb_substr() function can intercept some characters and return Just start intercepting the string from the 4th character (that is, the 5th character).<?php $str = "ABCDEFG"; echo $str . "<br>"; echo mb_substr($str,4); ?>Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of How to remove the first 4 characters from a php string. For more information, please follow other related articles on the PHP Chinese website!