How to deal with zero length strings in an array

This is a discussion on "How to deal with zero length strings in an array" within the PHP Forum section. This forum, and the thread "How to deal with zero length strings in an array are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Mar 7th, 2008, 16:24
Junior Member
Join Date: May 2007
Location: London
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
How to deal with zero length strings in an array

Hope someone can help.
I need to split a sting (a description of an image) read in from MySQL into separate words and present them as a table of check boxes (they'll be used to select keywords for the image)
I've got the code working as I need it but for one problem.
I use
Code: Select all
$temp_word_array         = explode(" ",$row[$row_count]);
to separate the words out from the string but have found that if there is a double space between words I end up with one or more zero length strings. For example;
Code: Select all
 word array array(17) {
  [0]=>
  string(1) "A"
  [1]=>
  string(3) "tug"
  [2]=>
  string(7) "pulling"
  [3]=>
  string(1) "a"
  [4]=>
  string(4) "ship"
  [5]=>
  string(4) "from"
  [6]=>
  string(3) "the"
  [7]=>
  string(8) "Penzance"
  [8]=>
  string(3) "dry"
  [9]=>
  string(5) "dock."
  [10]=>
  string(0) ""
  [11]=>
  string(3) "One"
  [12]=>
  string(2) "of"
  [13]=>
  string(1) "a"
  [14]=>
  string(6) "series"
  [15]=>
  string(2) "of"
  [16]=>
  string(3) "10."
}
There is a double space after the full stop after 'dock'
When other functions I use later on to clean up the data for what I call junk words ( of, at, the etc) and presenting the output, these functions see the zero length string, assume that's the end of the array and abort.
I have tried using the index key to step through my subsequent 'for' loop to hopefully skip over the zero length string and tried testing for a zero length string but nothing I try works.
This is obviously something there's a function or technique to deal with so could someone enlighten me please.
Thanks.
Phillip
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Mar 7th, 2008, 17:38
CloudedVision's Avatar
Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 1,255
Blog Entries: 9
Thanks: 2
Thanked 40 Times in 40 Posts
Re: How to deal with zero length strings in an array

Maybe something like this?

PHP: Select all


$new_word_array 
= array();

$key 0;
foreach(
$temp_word_array as  $value) {
   if(
$value!="") {
      
$new_word_array[$key] = $value;
      
$key++;
   }


__________________
Web Design And Development: Other Road Design | Problems with IE6?: KApp | My Blog: Only Nerds Allowed | Learning PHP? Lessons
Last Blog Entry: Hilarious Rapper (Jul 29th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Mar 7th, 2008, 19:53
Junior Member
Join Date: May 2007
Location: London
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to deal with zero length strings in an array

Bu**er. I'm sure I attempted something very like that as one of the first things I tried.
I obviously got something wrong as your method certainly works.
Sure beats all the complicated methods I've spent the last three days trying on and off.
Anyway I'm a happy bunny now (until I hit the next problem) so thank you very much for your swift and accurate reply.
Keen amateurs with no colleagues to talk to would be really up the creek without people like you helping out.
Thanks and Happy Easter.
Phillip
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Mar 8th, 2008, 16:05
Reputable Member
Join Date: Nov 2007
Location: India
Posts: 150
Blog Entries: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to deal with zero length strings in an array

Otherwise you could have use preg_replace() to replace double spaces with single space.
Last Blog Entry: Cross browser nuisance (Feb 11th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Mar 8th, 2008, 16:26
Junior Member
Join Date: May 2007
Location: London
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to deal with zero length strings in an array

Thanks for that. That was one of the functions I looked at when I was trying to do it the complicated way.
I didn't think it suitable for me as I couldn't be certain there's not a series of three, or four spaces lurking somewhere in the database which the pattern of the function would then miss (as it is looking for two spaces) and the code would therefore break again.
I dare say there's some regular expression someone could come up with that might work but when I look at doing those my head hurts.
CloudedVision's example solves the problem simply, and it will also catch any other reasons that an empty field occurs in the array for some as yet unforeseen circumstance.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Mar 12th, 2008, 15:10
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to deal with zero length strings in an array

Quote:
Originally Posted by RohanShenoy View Post
Otherwise you could have use preg_replace() to replace double spaces with single space.
I'd look at str_replace() first. Isn't it faster?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Mar 12th, 2008, 19:48
CloudedVision's Avatar
Moderator
Join Date: Feb 2008
Location: In My Own Little World
Age: 14
Posts: 1,255
Blog Entries: 9
Thanks: 2
Thanked 40 Times in 40 Posts
Re: How to deal with zero length strings in an array

my method is probably the slowest one, but it'll catch triple, quadruple, etc. spaces, unlike preg_replace and str_replace. (actually, you might be able to catch triple spaces with preg_replace, but i know nothing about regular expressions)
__________________
Web Design And Development: Other Road Design | Problems with IE6?: KApp | My Blog: Only Nerds Allowed | Learning PHP? Lessons
Last Blog Entry: Hilarious Rapper (Jul 29th, 2008)

Last edited by CloudedVision; Mar 12th, 2008 at 19:51.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Length problem solved, now width. Manic056 Starting Out 5 Jan 31st, 2008 02:33
3 column div layout, not same length tox0tes Web Page Design 8 Jun 28th, 2007 15:08
setting page length wmw Web Page Design 6 May 22nd, 2006 16:17
Sorting a new array from an existing array Ozeona Flash & Multimedia Forum 2 Sep 20th, 2005 08:43
array unable to check another array so as to be displayed Ozeona Flash & Multimedia Forum 1 Aug 5th, 2005 10:26


All times are GMT. The time now is 02:12.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42