str_replace()
FunctionYou can simply use the PHP str_replace()
function to strip or remove all spaces inside a string.
Let's take a look at the following example to see how it actually works:
<?php
$str = 'This is a simple piece of text.';
$new_str = str_replace(' ', '', $str);
echo $new_str; // Outputs: Thisisasimplepieceoftext.
?>
The above example will however only remove spaces. If you want to remove all whitespaces including tabs, newlines, etc. you can use the preg_replace()
function which perform a regular expression search and replace, as demonstrated in the following example:
<?php
$str = "This is a simple \npiece\tof text.";
$new_str = preg_replace("/\s+/", "", $str);
echo $new_str; // Outputs: Thisisasimplepieceoftext.
?>
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 [email protected]
How to install Samba share and transfer files between Windows and Linux
Samba is powerful and open-source tool f...How to Split a String into an Array of Characters in JavaScript
Use the split() Method The JavaScript...How to Trigger a Click on a Link Using jQuery
Use the jQuery click() Method You can...How to Capture Browser Window Resize Event in JavaScript
Use the addEventListener() Met...How to Append Values to an Array in JavaScript
Use the push() Method You can simply...