How to remove first and last space from string with PHP

trim()

While PHP development, developer needs to remove first and last space from string. So today I am going to tell you a PHP function. This function name is trim(). With the help of this function you can remove the space from first and last from a string.

For example, let’s take a variable and assign the string with spaces:

Then output will you receive in this form.

Hello string..

ltrim()

If you want to remove only left side spaces so you should use ltrim() function for example

And output is: Hello string..

But for kind of more information this function can remove characters also.

rtrim()

Same as ltrim If you want to remove only right side spaces or characters you can use rtrim() function.

And output is: Hello string..

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *