473,381 Members | 1,431 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,381 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 11191
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
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
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

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...
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?
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...
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 ...
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...
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. ...
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...
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...
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.