delete null elements from an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ClarkePeters
    New Member
    • Dec 2006
    • 14

    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:
    Code:
    $spacesCount= substr_count($ztagsdata,"  ");
      $spacesCount=$spacesCount/2;
      echo $spacesCount;
    for ($i=1;$i<$spacesCount;$i++)
    {
    $ztagsdata= str_replace("  ", " ", $ztagsdata);
    }
    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.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    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:

    Comment

    • ClarkePeters
      New Member
      • Dec 2006
      • 14

      #3
      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.

      Comment

      • ClarkePeters
        New Member
        • Dec 2006
        • 14

        #4
        my typo: s/b perl not pearl

        Comment

        • ClarkePeters
          New Member
          • Dec 2006
          • 14

          #5
          Code:
          $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!!

          Comment

          • ClarkePeters
            New Member
            • Dec 2006
            • 14

            #6
            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:
            Code:
            $mystring= implode(' ', $an_array);
            then use the above code to wipe out any extra spaces,

            then explode it back into an array
            Code:
            $myarray=explode(' ', $mystring)
            and the empty or null elements will be gone, right?

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              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:

              Comment

              • ClarkePeters
                New Member
                • Dec 2006
                • 14

                #8
                Great, got it!
                Thanks for the heads up on that one.
                :)

                Comment

                Working...