Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove first 4 characters of a string with PHP

People also ask

How can I remove last 4 characters from a string in PHP?

You can use the PHP substr_replace() function to remove the last character from a string in PHP. $string = "Hello TecAdmin!"; echo "Original string: " . $string .

How do I remove the first character of a string in PHP?

You can use the PHP ltrim() function to remove the first character from the given string in PHP.

How can I remove part of a string in PHP?

The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.


You could use the substr function to return a substring starting from the 5th character:

$str = "The quick brown fox jumps over the lazy dog."
$str2 = substr($str, 4); // "quick brown fox jumps over the lazy dog."

If you’re using a multi-byte character encoding and do not just want to remove the first four bytes like substr does, use the multi-byte counterpart mb_substr. This does of course will also work with single-byte strings.