Strip All Spaces Out of a String in PHP

Strip All Spaces Out of a String in PHP

In this post we will give you information about Strip All Spaces Out of a String in PHP. Hear we will give you detail about Strip All Spaces Out of a String in PHPAnd how to use it also give you demo for it if it is necessary.

Use the PHP str_replace() Function


You 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 npiecetof text.";
$new_str = preg_replace("/s+/", "", $str);
echo $new_str; // Outputs: Thisisasimplepieceoftext.
?>


 

Hope this and post will helped you for implement Strip All Spaces Out of a String in PHP. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

5  +  5  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US