Home >Backend Development >PHP Problem >How to remove the first 4 characters from a php string

How to remove the first 4 characters from a php string

青灯夜游
青灯夜游Original
2022-05-31 13:58:062588browse

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.

How to remove the first 4 characters from a php string

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");
?>

How to remove the first 4 characters from a php string

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);
?>

How to remove the first 4 characters from a php string

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);
?>

How to remove the first 4 characters from a php string

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!

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