473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 11194
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

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

Similar topics

19
by: deko | last post by:
I'm kind of lost on this one - I need to modify 2 files based on user input: $data_array = file($data_file); $counter_array = file($counter_file); // There is a line-for-line relationship...
7
by: peter | last post by:
Hello, for a C++ delete: class Test{}; Test * A = new Test; delete A; We don't use delete A; How can C++ compiler can tell there are 50 destructor to deallocate?
15
by: Roy Smith | last post by:
I understand that "delete xp" deletes a scalar object and "delete xp" deletes an array of objects, but what I don't understand is why you need to tell the compiler which you're doing. When you...
3
by: Brian Underhill via DotNetMonster.com | last post by:
I am trying to delete an element from an array. I have an array of 52 elements and I want to search for an element and delete it. Therefore, making it an array of 51 elements. is it just ...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
6
by: flash | last post by:
write a program that manipulates arrays of integers. The main program should call three functions: Insert, Delete, and Search. The Insert function should call a function Sort that sorts the array. ...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
7
by: ITAutobot25 | last post by:
My delete button is not working in my GUI and my due date is today before midnight. Can anyone show me how to correct this error? My assignment statement is below as well as 5 classes. InventoryGUI...
12
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.