PDA

View Full Version : Removing all whitespace from string


Ultragames
12-05-2004, 08:53 AM
I need to remove non-visible (whitespace) text from a string. Not just spaces, but Alt+255, line breaks, anything that results in something thats not a normal char.

I dont think ctype will help me here, i can't find any string functions that seem to remove ALL whitespace. Will some kind of pregmatch do it?

Thanks.

Fou-Lu
12-05-2004, 10:24 AM
preg_match will support the \s (whitespace) character, though I'm not certain it will grab your newlines as well. However, your internal options may be useful here as well.
What do you define as being your "normal" characters? Because it may be more useful and less complex to search via whats not considered normal instead.

xiaodao
12-05-2004, 02:37 PM
to remove white space, can use "trim()"?

marek_mar
12-05-2004, 03:48 PM
trim() removes them from the beginning and end of a string.

xiaodao
12-05-2004, 04:12 PM
how about you explode() the white space away then join the result together

raf
12-05-2004, 07:45 PM
just replace all characters you wanna get rid of (i'm not sure what you are talking about) by a '' with a simple str_replace.
Like

$arr_remove=array(' ', '<br />', '<p>'); // add all things you need replacing
$allvisible = str_replace($arr_remove, '', $your_initial_text);

nothing more complicated then that needed...

Ultragames
12-05-2004, 09:53 PM
THats the problem raf, is i dont know all the different types of white space there are. How can i use pregmatch or \s to do it?

Thanks guys.

marek_mar
12-05-2004, 10:09 PM
<?
$arr_remove=array(" ", "\n", "\r\n", "\r", "\t", "\0", "\x0B", "\xFF", '<br />', '<p>'); // add all things you need replacing
$allvisible = str_replace($arr_remove, '', $your_initial_text);
?>

raf
12-06-2004, 08:08 AM
THats the problem raf, is i dont know all the different types of white space there are. How can i use pregmatch or \s to do it?

Thanks guys.
I still don't understand what you are trying to do or what you are expecting from "use pregmatch or \s to do it".
I don't see any need for a regex (since you are replacing values and not valueformats) and i don't see what pattern you could think up for "Not just spaces, but Alt+255, line breaks, anything that results in something thats not a normal char" (although i don't realy understand what you mean by it ...)

Maybe you can save us some time and post a sample of expected input and desired output ...
Or try the code i posted and that marek_mar expanded with some other replacable values...

By the way, the elements of the array don't need to be doublequoted since they don't need parsing --> they need to be doublequoted (and parsed) when you output them like in an echo, but not when you are using them as values to check against.

levitymn
10-01-2009, 12:55 AM
by anything that is not a normal character, he means any character that does not result in a visible character being drawn by the standard ascii or ISO character set, this would include but not be limited to space, tab, new line, CR, etc...

He would like a regular expression that would eliminate all "White Space" characters, and by white space he means anything that does anything that does not generate a visible pixel when rendered as text.

skcin7
10-01-2009, 12:59 AM
Hey there. I just recently had the same problem. Here is the solution I found:

First, I used trim($string). This removed all whitespace at the beginning and end of my string.

Second, the string could still possibly have whitespace in the middle. I wanted any whitespace to reduce to a single space. The way I did it is like this:
$string = preg_replace( '/\s+/', ' ', trim( $string ) );
The block of code above will reduce any consecutive spaces to a single space.

Hope this helps!

kbluhm
10-01-2009, 01:22 AM
\s matches all white space, including tabs, etc, as well as new lines if you employ the `s` modifier:
$no_spaces = preg_replace( '/\s/s', '', $plenty_of_spaces );