remove first and last character from string php – How to remove the first character of string in PHP?

remove first and last character from string php – If string is not known and we want to remove characters from beginning then we can use substr(). Use the mb_subtr() function to remove characters from the end of the string.

remove first and last character from string php

You can use the substr() function if you want to remove last ‘n’ characters from a string. In this example, we are transforming the string “abcde” into “bcd”.

Remove Last ‘n’ Characters from a String Using substr()

$slogen = "An pakainfo a website keeps anyone away if you throw it hard enough.";

$deleted_last_one   = substr($slogen, 0, -1);
$deleted_last_two   = substr($slogen, 0, -2);
$deleted_last_three = substr($slogen, 0, -3);
$deleted_last_four  = substr($slogen, 0, -4);
$deleted_last_twenty  = substr($slogen, 0, -20);

// Result — An pakainfo a website keeps anyone away if you throw it hard enough
echo $deleted_last_one;

// Result — An pakainfo a website keeps anyone away if you throw it hard enoug
echo $deleted_last_two;

// Result — An pakainfo a website keeps anyone away if you throw it hard enou
echo $deleted_last_three;

// Result — An pakainfo a website keeps anyone away if you throw it hard eno
echo $deleted_last_four;

// Result — An pakainfo a website keeps anyone away if you t
echo $deleted_last_twenty;

Remove First ‘n’ Characters from a String Using substr()

$slogen = "An pakainfo a website keeps anyone away if you throw it hard enough.";

$deleted_first_one   = substr($slogen, 1);
$deleted_first_two   = substr($slogen, 2);
$deleted_first_three = substr($slogen, 3);
$deleted_first_four  = substr($slogen, 4);
$deleted_first_twenty  = substr($slogen, 20);

// Result — "n pakainfo a website keeps anyone away if you throw it hard enough."
echo $deleted_first_one;

// Result — " pakainfo a website keeps anyone away if you throw it hard enough."
echo $deleted_first_two;

// Result — "pakainfo a website keeps anyone away if you throw it hard enough."
echo $deleted_first_three;

// Result — "pple a website keeps anyone away if you throw it hard enough."
echo $deleted_first_four;

// Result — " anyone away if you throw it hard enough."
echo $deleted_first_twenty;

Remove Specific Characters from the End of String Using rtrim()

$slogen = "How do you do freedownload Towebsite????!!!,,,...   ";

// Result — "How do you do freedownload Towebsite????!!!,,,..."
echo rtrim($slogen);

// Result — "How do you do freedownload Towebsite????!!!,,,"
echo rtrim($slogen, ' .');

// Result — "How do you do freedownload Towebsite????!!!,,,..."
echo rtrim($slogen, ' ,');

// Result — "How do you do freedownload Towebsite????!!!"
echo rtrim($slogen, ' .,');

// Result — "How do you do freedownload Towebsite"
echo rtrim($slogen, '?!., ');

Remove Specific Characters from the Beginning of String Using ltrim()

$slogen = ",,,...!!!###???How do you do freedownload Towebsite?";

// Result — "...!!!###???How do you do freedownload Towebsite?"
echo ltrim($slogen, ',');

// Result — "!!!###???How do you do freedownload Towebsite?"
echo ltrim($slogen, '.,');

// Result — "...!!!###???How do you do freedownload Towebsite?"
echo ltrim($slogen, ',!');

// Result — "How do you do freedownload Towebsite?"
echo ltrim($slogen, '?!.,#');

Don’t Miss : Php Remove Last Character From String Code Example

I hope you get an idea about remove first and last character from string php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.