Forum Moderators: coopster

Message Too Old, No Replies

Splitting up Variables

         

wfernley

4:36 pm on Oct 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi I was curious how to take a phone number in the format (123) 123 - 1234 and split it up into 3 variables and taking out the (, ), - , and spaces.

Also I was curious how to check the length of a number.

Can anyone help me out?

Thanks :)

Wes

mincklerstraat

6:24 pm on Oct 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



looks like you're going to want to look at the string functions - in your case, strpos, substr, and strlen. If you aren't too ambitious, you can skip the regex stuff (such as preg_match and preg_replace).

Split your variables by finding the positions of these characters with strpos, and then selecting the part of the string you want on that basis with substr. trim might come in handy too.

The string functions: [be2.php.net...]

coopster

9:41 pm on Oct 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



preg_split()
would be handy here. Here it is in a function:

function phone_number($phone_number)
{
$pattern = "/[^\d]+/";
$phone_number = array_values [php.net](array_filter [php.net](preg_split [php.net]($pattern, $phone_number)));
if (count [php.net]($phone_number) < 3) array_unshift [php.net]($phone_number, '');
return $phone_number;
}

Then, to use it, just pass the function your somewhat properly formatted phone number:
$phone_numbers = array( 
'(123) 456-7890',
'123 456-7890',
'123 456 7890',
'(123) 456-7890',
' (123) 456-7890',
' (123)456-7890',
' (123) 456 - 7890',
'456 - 7890',
'() 456 - 7890',
'( ) 456 - 7890',
'456 7890',
'456-7890'
);
foreach ($phone_numbers as $phone_number) {
list [php.net]($area_code, $exchange, $number) = phone_number($phone_number);
print "area code: $area_code\nexchange: $exchange\nnumber: $number\n\n";
}
Having a look at the function, we find a pattern that says, "find anything that is not a number between 0 and 9".

Use

preg_split()
to split the string ($phone_number) on the pattern and put the captured values into an array; this array may have a blank in it in the very first array element. Why? Because the very first split value is going to be anything in front of the first non-numeric value found (such as the first parentheses of the area code, or an empty set of parentheses). In this case, it's nothing, and will always have to be nothing. Therefore, keep in mind that this function will work only if a somewhat properly formed phone number is passed to it. All the values in the array given work just fine. Use a regular expression to make sure you have a properly formatted phone number.

Next, use an

array_filter()
without a callback to get rid of the
NULL
value found in front of the area code, then the
array_values
simply restructures the numerical indexes of the array. This is important because
list
assumes that the numerical indices start at 0. Use
count
to figure out if there is an area code in the phone number or not. If not, throw an empty area code onto the front of the array using PHP's
array_unshift()
function (all numerical array keys will be modified to start counting from zero).