All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Trim Spaces Before And After

Last Updated : Mar 11, 2024

PHP Trim Spaces Before And After

In this tutorial we will show you the solution of PHP trim spaces before and after, we can remove space from sentence easily by manually finds space and returns removed sentence as a result but here we are going to see removing space from sentence before and after only.

We have option for removing space before and after in line of text using php inbuilt function.

The user defined text we need to give as parameter for trim() function then it returns space removed from line of sentence before and after.

Step By Step Guide On PHP Trim Spaces Before And After :-

For removing space here we used trim() function in php. The trim() function removes whitespace and other predefined characters from both sides of a string.

It has two type of trim function those are, ltrim() function used to remove whitespace or predefined characters from left side of a string only and rtrim() function used to remove whitespace or predefined characters from right side of a string only.

Then we used var_dump() function also it really useful for user verification because it returns count of characters in string include whitespace.

<?php
$words1 = ' hello ';
$words = trim($words1);
echo "Before Trim Space :";
var_dump($words);
echo "<br>After Trim Space :";
var_dump($words1);
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. We defined string and stored to variable ‘$words1’ and ‘$’ symbol must add before variable name when we define a variable in php.
  4. Our user defined variable value is ' hello ' and it can two or more words as a string we can give any string its our own choice.
  5. Variable ‘$words’ contain removed whitespace string by using trim() function with our input as parameter so it returns space removed string to variable ‘$words’.
  6. Var_dump() function is used to dump information about a variable. This function displays structured information such as type and value of the given variable. Array and objects are explored recursively with values indented to show structure. This function is also effective with expressions.
  7. So here we used for user can identify different between before removing space and after removed space from string because it gives output as count of characters of string including whitespace.

Conclusion :-

In conclusion we are able to know how to remove space from string before and after in php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When we executing this program on browser it returns whitespace removed from before and after string to webpage and as we see earlier ltrim() and rtrim() functions also available in php we can use those when we need in future.

I hope this tutorial on PHP trim spaces before and after helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪