Friday, October 24, 2025
HomeLanguagesHow to remove the first character of string in PHP?

How to remove the first character of string in PHP?

Remove the very first character of a given string in PHP

Examples:

Input : Geeksforneveropen
Output : eeksforneveropen

Input :, Hello geek!
Output : Hello geek!

Explanation:
In PHP to remove characters from beginning we can use ltrim but in that we have to define what we want to remove from a string i.e. removing characters are to be known.
Example:




<?php
    $str = "neveropen";
  
    // Or we can write ltrim($str, $str[0]);
    $str = ltrim($str, 'g');
  
    echo $str;
?>


Output:

eeks

If string is not known and we want to remove characters from beginning then we can use substr(). Here we can use it by two parameters one is the string and the other is the index. substr() return string from the second parameter index to the end of the string.




<?php
    $str = "neveropen";
    $str1 = substr($str, 1);
    echo $str1."\n";
    $str1 = substr($str, 2);
    echo $str1; 
?>


Output:

eeks
eks

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS