I'm trying to split this phone number: (800) 888-9999 into 3 different text fields in a form, and I'm having problem spliting the parenthesis, $phone = "(800) 888-9999"; list($phone1,$phone2,$phone3) = split('[() -]',$phone); didn't work... What's the correct way to split all these different characters?
Thanks
Hm. I'd just split on strings of consecutive non-digits
split('[0-9]+',$phone)
Sure, it will split "}800WOW441-zot-9888" into 800, 441 and 9888, but is that a crime?
stupid me... why didnt I think of that... but it splited into 2 form text fields instead of 3. How should I split the space? (800) 441-9888
I think it split it into four fields, including an empty one at the start.
I knew I should have recommended preg_split when I had the chance:
preg_split('/\D+/',$phone,-1,PREG_SPLIT_NO_EMPTY)
Would you care to explain to me what this does? I dont understand "\D+" and "-1", what do they mean?
Thank you very much. newbie Eric
Well, they're in the manual, but
\D+ - one or more non-digits
-1 - limit of "-1" matches; it doesn't really mean much, it's just a place-filler so PHP doesn't miscount the fourth argument as the third.