Jump to content

[SOLVED] Spliting 10 Digit Phone Number


The14thGOD

Recommended Posts

Does PHP have a function that can split based on a number of digits? Like take for instance 5555555555 i need to split it into 3 parts 555-555-5555 essentially. So far all the split's I've looked at on PHP.net need some kind of character to split based on. I just want to split after the 3rd number(so 2 if it's treated like an array) etc.

 

Thanks for any help.

 

Justin

Link to comment
Share on other sites

If you know for sure you'll get 10 digits like that, you can always just grab the first 3 and put in an array, then the 2nd 3 and put in an array, then the last 4.

 

You'll just have to make sure they dont use any () or - in there.

Link to comment
Share on other sites

<?php
function valid_phone($phone)
{
  //validate password
  if(ereg("^[0-9]+$",$phone) && strlen($phone) = 10)
  return true;
  else
  return false;
}
$phone = $_POST['phone'];
if(!valid_phone ($_POST['phone'])) {
$error = "Invalid Phone Number";
}
else {
echo "(" . $phone[0] . $phone[1] . $phone[2] . ")" . $phone[3] . $phone[4] . $phone[5] . "-" . $phone[6] . $phone[7] . $phone[8] . $phone[9];
}
// If The Phone number entered was 5555555555 it will produce (555)555-5555
// It will also make sure the number is 10 digits long and only numeric
?>

Link to comment
Share on other sites

I wrote this a while back to read phone numbers of varying types, hoping php 6 has some global function similar to this.

 

<?php
//Formats a phone nubmer
function phone_format($number){
$pattern = "[^0-9]";
//Strip number down to just digits the delimter will be added in post
$number = preg_replace($pattern,"",trim($number));
$len = strlen($number);
if($len == 11){
	$output = substr($number,0,1)."(".substr($number,1,3).")".substr($number,4,3).".".substr($number,7,4);
}
elseif($len == 10){
	$output ="(". substr($number,0,3).")".substr($number,3,3).".".substr($number,6,4);
}
elseif($len == 7){
	$output = substr($num,0,3).".".substr($num,4,3);
}
elseif($len == 0){
	$output = "None";
}
else{
	$output = $number;
}
return $output;
}
?>

The parameter is just any integer or string as it strips all non zeros any way.

Link to comment
Share on other sites

yeah that is the fun of php, but mine actually doesn't care if it doesn't match a pattern it just will remove all the non integers and reformat it if it follows a length like:

 

11 digit: 1(555)555.5555

10 Digit: (555)555.5555

7 Digit: 555.5555

no Match: returns string stripped of its non integers

Link to comment
Share on other sites

My PHP Intellegence is just how good on a scale of 1-10 I think I am. So I know about 47% of all of the PHP functions and I know how to incorperate them into any situation. Its not a test or anythingh fancy I took its just on a scale of how good I think I am, just so people who hire me for freelancing can see how good I think my skills are in the language.

Link to comment
Share on other sites

Well it figures you gotta start it off with something really easy and make it harder as you go along. You also need like 3 questions per level so it choses a random one so no-one can cheat. But it figures you should start off with what does the echo statement do? Then:

rand

strtoupper

strtolower

array()

$str[3]

And ect ect...

Link to comment
Share on other sites

that or I was thinking of doing it like in gorups

first a series of syntaxical errors

 

Then array questions

 

String Mainipulations

 

MySQL

 

Integers

 

Etc. etc.

 

And each time you take it there are 10 questions to each section based on how fast you answer and if its correct a raw score is developed.  so it be 50 questions max points of 10 each a perfect score is 500, but that requires you to answer the questions quickly, if you answer everything right the lowest would be like 250.

Link to comment
Share on other sites

Guest
This topic is now 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.