Your own trim, rtrim and ltrim functions in PHP

While we already have trim(), ltrim() and rtrim() in php, here is how we can implement the same feature. These are listed here just for understanding the process. In practice, you are recommended to use the built-in functions for performance and consistency reasons.

my_trim() will remove specified characters from both ends of your string, and in case no such characters are specified as second argument to the function, will remove white-space from both ends of the provided string.

my_ltrim() will remove specified characters from start of your string, and in case no such characters are specified, will remove just white-space from start.

my_rtrim() will remove specified characters from end of your string, and in case no such characters are specified, will remove just white-space from the end.

Testing with your input: