[PHP] need help w/ Split()

2001-03-08 Thread Scott Walter

I am attempting to take a line of text (a list) that has been entered into 
a form and split it into the appropriate parts, placing them into an 
array.  I am splitting the input on commas, semi-colons, and spaces.

Problem 1:  Is with "white space".  If the input has spaces after a comma, 
it splits on the comma, but enters each "space" as a separate element of 
the array.

Problem 2: Is with "white space" also.  If the input has just spaces 
between the appropriate parts, then it will split on the first "space", but 
enter the rest of the "spaces" as separate elements of the array.

Here my code:
$course_list = preg_split("/[\,\;\s*]/", $input_list);
for ($i=0; $icount($course_list); $i++) {
echo("Item $i: $course_list[$i]br\n");
}

Which on input "0101, 0102,  0103" produces [0101][ ][0102][ ][ ][ ][0103]

Any help would be appreciated!!


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




RE: [PHP] need help w/ Split()

2001-03-08 Thread Nathaniel Hekman

Your regex is incorrect.  You've written:

/[\,\;\s*]/

That * means "match a *" because it's inside the brackets.  Put it outside,
like this (actually use a + instead):

/[\,\;\s]+/

to match 1 or more of any of those characters.

That may not be exactly what you want, since that will also cause this:

input "0101, 0102,0103" == [0101][0102][0103]

If you want repeated commas to create empty elements, then try something
like this:

/\s*[\,\;\s]\s*/

I haven't tried that, but it looks right at first glance...  Optional white
space, followed by exactly one of ',; ', followed by optional white space.


Nate

-Original Message-
From: Scott Walter [mailto:[EMAIL PROTECTED]]
Sent: Thursday, March 08, 2001 10:44 AM
To: [EMAIL PROTECTED]
Subject: [PHP] need help w/ Split()


I am attempting to take a line of text (a list) that has been entered into 
a form and split it into the appropriate parts, placing them into an 
array.  I am splitting the input on commas, semi-colons, and spaces.

Problem 1:  Is with "white space".  If the input has spaces after a comma, 
it splits on the comma, but enters each "space" as a separate element of 
the array.

Problem 2: Is with "white space" also.  If the input has just spaces 
between the appropriate parts, then it will split on the first "space", but 
enter the rest of the "spaces" as separate elements of the array.

Here my code:
$course_list = preg_split("/[\,\;\s*]/", $input_list);
for ($i=0; $icount($course_list); $i++) {
echo("Item $i: $course_list[$i]br\n");
}

Which on input "0101, 0102,  0103" produces [0101][ ][0102][ ][ ][
][0103]

Any help would be appreciated!!


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] need help w/ Split()

2001-03-08 Thread Christian Reiniger

On Thursday 08 March 2001 18:43, you wrote:
 I am attempting to take a line of text (a list) that has been entered
 into a form and split it into the appropriate parts, placing them into
 an array.  I am splitting the input on commas, semi-colons, and spaces.

 Problem 1:  Is with "white space".  If the input has spaces after a
 comma, it splits on the comma, but enters each "space" as a separate
 element of the array.

 Problem 2: Is with "white space" also.  If the input has just spaces
 between the appropriate parts, then it will split on the first "space",
 but enter the rest of the "spaces" as separate elements of the array.

 Here my code:
   $course_list = preg_split("/[\,\;\s*]/", $input_list);

You can't put the asterisk in the character class - it's taken as literal 
asterisk in there.

try preg_split("/([\,\;]\s*)|(\s+)/", $input_list);

i.e. "either (',' or ';' eventually followed by spaces) or (at least one 
space)"

-- 
Christian Reiniger
LGDC Webmaster (http://sunsite.dk/lgdc/)

...1000100011010101101010110100111010113...

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]