Regular Expressions (removing extra white spaces)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rafael Almeida
Forum Newbie
Posts: 5
Joined: Thu Dec 16, 2004 10:21 pm

Regular Expressions (removing extra white spaces)

Post by Rafael Almeida »

Hello all!

What is the regular expression pattern to remove extra white spaces in the middle of strings (leaving just one)?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

replace 2 or more consequtive spaces w/ a single space

Code: Select all

<?php
$pattern = '/(\s{2,})/';
$subject = 'foo      bar     baz';

echo preg_replace($pattern, ' ', $subject);
?>
Post Reply