php Replacing multiple spaces with a single space
Use preg_replace()
and instead of [ \t\n\r]
use \s
:
$output = preg_replace('!\s+!', ' ', $input);
From Regular Expression Basic Syntax Reference:
\d, \w and \s
Shorthand character classes matching
digits, word characters (letters,
digits, and underscores), and
whitespace (spaces, tabs, and line
breaks). Can be used inside and
outside character classes.
replace multiple spaces in a string with a single space
Your regex replaces any number of spaces (including zero) with a space. You should only replace two or more (after all, replacing a single space with itself is pointless):
$s = preg_replace("/ {2,}/", " ", $x);
How to correctly replace multiple white spaces with a single white space in PHP?
When passing u
modifier, \s
becomes Unicode-aware. So, a simple solution is to use
$new_str = preg_replace("/\s+/u", " ", $str);
^^
See the PHP online demo.
How to replace multiple   and whitespace with one   and shrink whitespace only chunks to a space?
You may use
$find = ['/^((?: |\s)+)|(?1)$/', '/\s* (?:\s* )*\s*/', '/\s+/'];
$replace = ['', ' ',' '];
$text = preg_replace($find, $replace, trim($text));
See the PHP demo.
Details
'/^((?: |\s)+)|(?1)$/'
=>''
removes(?1)
here just recurses(?: |\s)+
and is here to make the pattern a bit shorter)'/\s* (?:\s* )*\s*/'
=>' '
replaces all sequences of whitespace and'/\s+/'
=>' '
replaces 1+ whitespaces with 1 regular space.
Php replace multiple spaces with single spaces
Using preg_replace()
$name = preg_replace('#\s+#', ' ', $rawName);
php replace multiple spaces with a single space
Try this one. It will display as a single space on browser
$output = str_replace(" ", " ",$input);
replacing spaces with just one _
Probably the cleanest and most readable solution:
preg_replace('/[[:space:]]+/', '_', $name);
This will replace all spaces (no matter how many) with a single underscore.
Replace Multiple Spaces and Newlines with only one space in PHP
I would encourage you to use preg_replace
:
# string(45) "This is a dummy text . I need to format this."
$str = preg_replace( "/\s+/", " ", $str );
Demo: http://codepad.org/no6zs3oo
You may have noted in the " . "
portion of the first example. Spaces that are immediately followed by punctuation should probably be removed entirely. A quick modification permits this:
$patterns = array("/\s+/", "/\s([?.!])/");
$replacer = array(" ","$1");
# string(44) "This is a dummy text. I need to format this."
$str = preg_replace( $patterns, $replacer, $str );
Demo: http://codepad.org/ZTX0CAGD
replace multiple spaces by non breaking spaces
It seems all you need is to replace each whitespace that is preceded with another whitespace symbol. Use a lookbehind-based approach:
(?<=\s)\s
See the regex demo.
The (?<=\s)
is a positive lookbehind that requires the presence of a whitespace immediately before the current location, but the whitespace is not consumed, and is thus not replaced.
Below is a PHP demo:
$s = "This is my text.";
echo preg_replace('~(?<=\s)\s~', ' ', $s);
// => This is my text.
Related Topics
List All Files in One Directory PHP
Error Message Strict Standards: Non-Static Method Should Not Be Called Statically in PHP
How to Prevent Code Injection Attacks in PHP
PHP Mail() Function on Localhost
How to Create and Download a CSV File from PHP Script
How to Avoid Echoing Character 65279 in PHP
Lamp: How to Create .Zip of Large Files For the User on the Fly, Without Disk/Cpu Thrashing
Amazon S3 - How to Fix 'The Request Signature We Calculated Does Not Match the Signature' Error
On Delete Cascade With Doctrine2
How to Implement a Web Scraper in PHP
How Can Strip Whitespaces in PHP'S Variable
PHP Login System: Remember Me (Persistent Cookie)
PHP: Access Array Value on the Fly
MySQL Join Two Tables With Comma Separated Values
Setting the PHP Default Encoding to Utf-8