Home » » Remove extra spaces from string in php

Remove extra spaces from string in php

Written By Unknown on Rabu, 11 April 2012 | 08.33

There are times when we have strings with adjacent spaces in between. And we want to remove these unwanted spaces and the trim() function doesn't work as it trims only from start and end.

This can be done with a simple preg_replace() function call. Look at the code..



$s = preg_replace("/\s+/"," ",html_entity_decode($string));


Example call

$string = "This    is    the      text     !!";
$string = preg_replace("/\s+/"," ",html_entity_decode($string));
echo $string;

This code will remove all adjacent spaces from the string including   spaces as well.
Share this article :

Posting Komentar

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Kumpulan Kata Broadcast Blackberry - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger