Jump to content

[SOLVED] Probably really easy......


dumdumsareyum

Recommended Posts

...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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.