How to remove white space from a string in PHP

1281 views 2 years ago PHP

Use the PHP trim() function

You can use the PHP trim() function to remove whitespace including non-breaking spaces, newlines, and tabs from the beginning and end of the string. It will not remove whitespace that occurs in the middle of the string. Let's take a look at an example to understand how it basically works:

<?php
$my_str = '    Welcome to Tutorial Republic ';
echo strlen($my_str); // Outputs: 33
$trimmed_str = trim($my_str);
echo strlen($trimmed_str); // Outputs: 28
?>

i hope it can help a lot.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com