Eliminating ALL Extra White Space

Is there any way of getting rid of all extra white space in a string? Not just trimming it, but actually getting rid of it? I know that PHP automatically prints the string with only 1 character of white space in between each word, but when you use the

 tag you find that it is still stored in the string, and I wish to use the white space as a delimiter (or if there is any other way of acheiving the same effect, please tell me)

Are you trying to remove the white space from within a string, not just the ends? Of course the ends you can use TRIM() or LTRIM() and RTRIM().

I don’t know if there is a specific function designed to remove extraneous white space in the middle of the string, but you could certainly do it manually with things like substr() and str_pos() and concatenate your string back together (now stripped of extra spaces). Or you could do a similar loop with EXPLODE(). You could explode the string into an array (Space delimited) and then loop through the array and recreate the string.

I know it’s not the easiest, however it would get the job done.

Maybe Bane has a better idea on this as well.

Good Luck.

Yes, I do mean from the middle of the string as well.

Thanks for your help, those ideas sound like they would work, but not very efficiently… :o

You could try to just use str_replace(). http://www.php.net/str_replace

I was actually going to suggest the str_replace as well (as I was reading another post about it.).

PHP offers so many functions… it’s tough to remember them all (of course it’s not like I try to remember them all).

Thanks Ragster for your help on this one.

No problem Paul, I don’t remember them all either, but I had just helped someone on this same type of issue and I have used that function many of times. That is the only reason I even thought of it. :)

How would you do that with white space?

The following should do the trick, just replace $string with your variable and you should be able to get rid of all instances of white space:

$string = str_replace(" ","",$string);

Break down of function

The first parameter of " " is what the function needs to look for.

The second parameter of “” is what it is to replace it with.

The third parameter is the variable in which to make the changes.

But what if I do want to keep one unit of white space to be used as a divider?

Ok, basically what I want to do is this.

  1. Get the user to copy and paste information which is divided by an unknown number of spaces/tabs.

  2. Extract any “words” (divided by spaces) which contain letters of the alphabet in (there can also be numbers and underscores)

  3. Print these to the browser with commas in between.

I hope this makes it a bit clearer

To ensure you remove all the extraneous white space you can do a quick loop.

[php]
While (strpos($StringVariable, " “) {
$StringVariable = str_replace(” “,” ",$StringVariable);
}
[/php]

What you have is the WHILE loop checks the string for an occourance of a DOUBLE SPACE. If it finds one it enters the loop and replaces ALL occurances of DOUBLE SPACEs with a SINGLE SPACE.

Problem… You have something that is a TRIPPLE SPACE. So now str_replace finds a single occurance of a DOUBLE SPACE and a single occurance of a SINGLE SPACE. It replaces the first 2 spaces (as a double space) with a single space. Then it moves on. No more occurances of a DOUBLE SPACE are found. But it leaves behind a SINGLE SPACE right next to a SINGLE SPACE (thus another Double Space).

That is why you need the loop.

I know it’s not clean cut but it indeed DOES work.

I tried it on my machine and I got exactly the results I expected…

[php]

<? //With the WHILE loop $StringVariable = "1 2 3 4"; While (strpos($StringVariable , " ")) { $StringVariable = str_replace(" "," ",$StringVariable); } echo "
 $StringVariable 

"; // without the While loop $StringVariable = "1 2 3 4"; $StringVariable = str_replace(" "," ",$StringVariable); echo "
 $StringVariable 
"; ?>

[/php]

I managed to kind of bypass it altogether with this code:

[php]
$works_please = explode( " ", $inputtext );

foreach ( $works_please as $temp ) {

if ( preg_match( “/[a-zA-Z]/”, $temp, $notneeded ) ) {

$newarray[] = $temp;

}

}

foreach ( $newarray as $temp2 ) {

if( $newarray ) {
if( $temp2 != “(online)” ){
print ", ";
trim ($temp2);
print $temp2;
}
}

}[/php]

My only problem now is that I dont want the comma to appear before the first instance of $temp2.

One way you could take your above you could add some simple counter/loop (the $i portion)

[php]
$works_please = explode( " ", $inputtext );

foreach ( $works_please as $temp ) {
if ( preg_match( “/[a-zA-Z]/”, $temp, $notneeded ) ) {
$newarray[] = $temp;
}
}

$i = 0;
foreach ( $newarray as $temp2 ) {
if( $newarray ) {

    if( $temp2 != "(online)" ){
        if ($i > 0) print ", ";
        $i++;
        trim ($temp2);
        print $temp2;
    }
}

}
[/php]

Seems to me that the WHILE loop I presented (a couple of posts ago) would have been easier… but either way.

Good luck. Hope this helps.

Thanks for your help :D

The while loop seems that it would work, but I dont want to change too much.

This code does not seem to be working, could anyone say why?:

[php]

<?php if ( isset ( $hidden ) ) { print "

Processed

"; $separated = explode( " ", $inputtext, $notneeded ); foreach ( $separated as $temp ) { if ( preg_match( "/[a-zA-Z]/", $temp ) ) { $newarray[] = $temp; } } $i = 0; foreach ( $newarray as $temp2 ) { if ( $newarray ) { if ( $temp2 != "(online)" ) { if ( $i > 0 ) { print ", "; } $i++; trim ( $temp2 ); print $temp2; } } } } else { ?> <?php } ?>

[/php]

Why is it not working? Are there error messages? What is it doing? What is it NOT doing?

Help us help you. Don’t present code and tell us it’s not working and expect us to debug your code.

Please read http://www.phphelp.com/forums/viewtopic.php?t=4089.

I posted my aims above, this is just an update :lol:

I want it only to present a list of the “words” which contain at least 1 letter of the alphabet, separated by commas

As for the errors, it shows numbers (without any letters), and there are no commas at all.

Dont worry, it seems to be working now 8)

Sponsor our Newsletter | Privacy Policy | Terms of Service