How to remove whitespace from string using php

Here we are going to show you how to remove and replace single white space from a string using a predefined function preg_replace().

$string = "This is  simple string with  multiple   white   space";
echo preg_replace('/\s+/', '-', $string);

Here i showing you how to remove more then one or multiple white-space and replace each with only one space. Remove multiple white-space simply we have to use a regular expression.

Example: Take all of the spaces of the string and replace each with only one space

$string = "This is  simple string with  multiple   white   space";
$replaced = preg_replace('/\s\s+/', ' ', $string);

Tags: How to remove multiple white-space and replace with one space using php

php remove whitespace, remove space from url in php, remove specific characters from string php, php strip crlf, php remove double spaces, php remove carriage return, php trim characters from start of string, remove duplicate white spaces php

Leave a Reply

Related Posts: You may like

Extract Total Number of Image And Missing Alt Attribute using php

How to extract Heading tags using PHP from string

Remove All Special Characters From String PHP

Redirecting HTTP to HTTPS with PHP

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Download Instagram Videos & Photos.

Download Now

CURRENCY CONVERTER

Real Time Currency Converter

Convert Now

ROBOST.TXT GEN.

Robots.txt File Creater

Create Now

HTML MINIFIER

Compress yout HTML Code

Minify Now

CSS MINIFIER

Compress yout CSS Code

Minify Now

JAVASCRIPT MINIFIER

Compress yout JavaScript Code

Minify Now

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.