469,271 Members | 1,685 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

delete null elements from an array

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
7 10859
ronverdonk
4,258 Expert 4TB
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
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
my typo: s/b perl not pearl
Jan 3 '07 #4
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
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
4,258 Expert 4TB
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
Great, got it!
Thanks for the heads up on that one.
:)
Jan 4 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

15 posts views Thread by Roy Smith | last post: by
3 posts views Thread by Brian Underhill via DotNetMonster.com | last post: by
29 posts views Thread by Jon Slaughter | last post: by
7 posts views Thread by ITAutobot25 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.