GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Dec-2003, 14:49
samtediou samtediou is offline
New Member
 
Join Date: Jul 2003
Posts: 18
samtediou is an unknown quantity at this point

Extra null element in an array


Hello forum,

I've got a little bug I haven't seen before. I'm reading a list of collocations (phrases of two or more words) from a file and exploding the file string into an array. So far so good.

The collocations are phrases whose components are separated by underscores (_) instead of spaces. Here are some examples of the collocations I'm reading into my collocation array. (Hyphens are also permitted.)


12-tone_music
12-tone_system
14_july


Then, I run through each element in my COLLOCATION array and separate each collocation into a COMPONENT array. I wrote my script to read each character in the collocation string and do the following:

1. If character is not a "_" and not a "-", concatenate character to a temporary word variable

2. Else, once an underscore or hyphen is found, save the concatenated word to a COMPONENT array ($aryComp).

This is what I would like to see for, say, 12-tone_music:

PHP Code:

$aryComp[0] = 12
  $aryComp[1] = tone
  $aryComp[2] = system 



What I'm finding, however, is that my arrays look like this:

PHP Code:

$aryComp[0] = 12
  $aryComp[1] = tone
  $aryComp[2] = 
  $aryComp[3] = system 



It doesn't matter where the underscore is in a collocation, if it is between the 1st and 2nd component, or the 2nd and 3rd component, and so on.

Does anyone know of any problems with the underscore and php files or arrays?

Thanks,

Samtediou
  #2  
Old 08-Dec-2003, 03:53
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Splitting a string into arrays - using preg_split()


It's hard to say without looking at your code or function that handles the 'splitting' of those strings...

Here's what I would suggest:
PHP Code:

<?php

// a custom function...
function build_component_array( $str )
{
  // returns an ARRAY   
  return( preg_split('/[-_]+/', $str) );
}

// example...
$test  = '12-tone_system';
$aryComp = build_component_array( $test );

/* $aryComp should result in
Array
(
    [0] => 12
    [1] => tone
    [2] => music
)
*/
?>

  #3  
Old 11-Dec-2003, 12:52
samtediou samtediou is offline
New Member
 
Join Date: Jul 2003
Posts: 18
samtediou is an unknown quantity at this point

Great!!


Thanks JdS,

Much better now.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting a 2D array c++ mike3340 C++ Forum 4 15-Dec-2003 14:35
deleting from an array ionyka C++ Forum 1 02-Sep-2003 04:09
join problem zuzupus MySQL / PHP Forum 0 14-Aug-2003 06:11
MySQL Syntax Error DropZite MySQL / PHP Forum 3 09-Jul-2003 05:00

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:28.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.