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? Link to comment https://forums.phpfreaks.com/topic/104004-solved-probably-really-easy/ 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]; Link to comment https://forums.phpfreaks.com/topic/104004-solved-probably-really-easy/#findComment-533253 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 Link to comment https://forums.phpfreaks.com/topic/104004-solved-probably-really-easy/#findComment-533586 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.