Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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