dumdumsareyum Posted May 3, 2008 Share Posted May 3, 2008 ...but I still can't figure it out. I haven't had much experience with string manipulation. I have some phone numbers coming from a database that are stored like this: (012)345-6789 I would like to somehow extract the area code from between the parenthesis and have two strings, one that looks like 012 and one that looks like 345-6789. I tried this for starters: $phones = preg_split( "\(...\)", $primaryPhone); but that made the program angry and it said: Warning: preg_split() [function.preg-split]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\PlanHouse\EditDesigner.php on line 47 Help please? Quote Link to comment Share on other sites More sharing options...
markjoe Posted May 5, 2008 Share Posted May 5, 2008 Here's my untested guess... preg_match("/\((\d{3})\)(\d{3}-\d{4})/",$input, $regex); $areaCode=$regex[1]; $phoneNumber=$regex[2]; Quote Link to comment Share on other sites More sharing options...
dumdumsareyum Posted May 5, 2008 Author Share Posted May 5, 2008 well, i just went ahead and wrote a little function to do it function ExtractPhone($phone) { $areaCodeAdd = "off"; $phoneArray = str_split($phone); foreach ($phoneArray as $number => $value) { if($value == '(') { $areaCodeAdd = "on"; } elseif($value == ')') { $areaCodeAdd = "off"; } elseif($areaCodeAdd == "on") { $areaCode .= $value; } elseif($areaCodeAdd == "off") { $phoneNum .= $value; } } //end of foreach $phoneNumber = array("areaCode" => $areaCode, "phone" => $phoneNum); return $phoneNumber; } //end of function Extract Phone Maybe I will try again with the regex once I learn some more Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.