364,088 Members | 5545 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

delete null elements from an array

ClarkePeters
P: 14
I have large text files that I read into an array, but before that I take out all the special characters such as tabs, new lines, and returns. However I'm left with all the extra spaces (sometimes as many as 20 or more at a time) because my users are allowed to type spaces in their text. For storage and other manipulation, I don't need all those spaces.

If I explode it I'm left with thousands of empty (null?) elements in the array.

I can take the spaces out before exploding by doing:
Expand|Select|Wrap|Line Numbers
  1. $spacesCount= substr_count($ztagsdata,"  ");
  2.   $spacesCount=$spacesCount/2;
  3.   echo $spacesCount;
  4. for ($i=1;$i<$spacesCount;$i++)
  5. {
  6. $ztagsdata= str_replace("  ", " ", $ztagsdata);
  7. }
which replaces double spaces with single spaces through a loop--which is a bit overkill (but it works), because i can't just replace double spaces with "" because then there's no space between each legitimate word)

any suggestions how to get out the extra spaces (and still leave a single space)? Or should I go ahead and explode into an array and then delete the null (empty?) values--which I don't know how to do. I know pop and push but can't find how to delete a middle element....

any suggestions are welcomed.
Jan 2 '07 #1
Share this Question
Share on Google+
7 Replies


ronverdonk
Expert 2.5K+
P: 4,047
Why don't you just use a simple regular expression to remove all extra blank spaces from your string? Like this one:[php]$string = preg_replace('/\s\s+/', ' ', $string);[/php]
Ronald :cool:
Jan 2 '07 #2

ClarkePeters
P: 14
Good heavens! that was so easy.
It worked like a charm.
(I'll take another look at pearl regular expressions to see exactly how that works).

Thank you so much Ronald!

Thank you.
Jan 3 '07 #3

ClarkePeters
P: 14
my typo: s/b perl not pearl
Jan 3 '07 #4

ClarkePeters
P: 14
Expand|Select|Wrap|Line Numbers
  1. $mystring = preg_replace('/\s\s+/', ' ', $mystring);
fyi to anyone who cares.

whitespace is represented by: \s

a single space can be represented by: ' '

so, a loose translation of the above code would be:
replace any space (\s) that is followed by multiple spaces (\s+) with a single space (' ') in the string named $mystring, and put the results back into $mystring.

Thanks again Ronald!!
Jan 3 '07 #5

ClarkePeters
P: 14
and....
I think this also means that if you have an array with null or empty elements you can implode it with a space separator ie:
Expand|Select|Wrap|Line Numbers
  1. $mystring= implode(' ', $an_array);
then use the above code to wipe out any extra spaces,

then explode it back into an array
Expand|Select|Wrap|Line Numbers
  1. $myarray=explode(' ', $mystring)
and the empty or null elements will be gone, right?
Jan 3 '07 #6

ronverdonk
Expert 2.5K+
P: 4,047
A null is not identical to a blank char. A null is the absence of a value, like void. It has no data type and no value. A blank is a real character, it has a data type CHAR and a value blank.

Ronald :cool:
Jan 3 '07 #7

ClarkePeters
P: 14
Great, got it!
Thanks for the heads up on that one.
:)
Jan 4 '07 #8

Post your reply

Help answer this question



Didn't find the answer to your PHP question?

You can also browse similar questions: PHP