The14thGOD Posted December 13, 2007 Share Posted December 13, 2007 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 More sharing options...
revraz Posted December 13, 2007 Share Posted December 13, 2007 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 More sharing options...
The14thGOD Posted December 13, 2007 Author Share Posted December 13, 2007 I made it so that the user knows they have to input a 10 digit phone number and if they try to put in 11 they can't (the maxlength attribute). I'm not too good with arrays but I'll try to look into it. Thanks, Justin Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 13, 2007 Share Posted December 13, 2007 You can use the substr() function: <?php $pn = '8885551212'; $pn1 = substr($pn,0,3) . '-' . substr($pn,3,3) . '-' . substr($pn,6,4); echo $pn1; ?> Ken Link to comment Share on other sites More sharing options...
Crew-Portal Posted December 13, 2007 Share Posted December 13, 2007 <?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 More sharing options...
cooldude832 Posted December 13, 2007 Share Posted December 13, 2007 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 More sharing options...
Crew-Portal Posted December 13, 2007 Share Posted December 13, 2007 Wow we all three posted at like Identical times. PHP is so cool because it has so many ways to do the same thing. Each way has its pro's and con's though! Link to comment Share on other sites More sharing options...
cooldude832 Posted December 13, 2007 Share Posted December 13, 2007 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 More sharing options...
Crew-Portal Posted December 13, 2007 Share Posted December 13, 2007 ya the way mine works is it has to be 10 char long or else you get an error message. Then it strips it down and add brackets and dashes and stuff (123)456-7890 Link to comment Share on other sites More sharing options...
The14thGOD Posted December 13, 2007 Author Share Posted December 13, 2007 wow. thanks for the help guys hah. I went away to find a solution and come back and theres three more. thanks again justin Link to comment Share on other sites More sharing options...
cooldude832 Posted December 13, 2007 Share Posted December 13, 2007 where you find your php intelligence in your signature? Link to comment Share on other sites More sharing options...
Crew-Portal Posted December 13, 2007 Share Posted December 13, 2007 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 More sharing options...
cooldude832 Posted December 13, 2007 Share Posted December 13, 2007 so its kinda pointless number since you are grading your self . Also I doubt you can name 47% of the php functions that are globally declared because a ton of libraries exist that are documented that no one ever uses (like IMAP GeoIP MsSQL ODBC Oracle etc.) Link to comment Share on other sites More sharing options...
Crew-Portal Posted December 13, 2007 Share Posted December 13, 2007 ya its kinda pointless but oh well whatever... Link to comment Share on other sites More sharing options...
cooldude832 Posted December 13, 2007 Share Posted December 13, 2007 I was just curoious case I wanted to get my number back, maybe we should write one Link to comment Share on other sites More sharing options...
Crew-Portal Posted December 13, 2007 Share Posted December 13, 2007 I was just curoious case I wanted to get my number back, maybe we should write one Write one what?... A Thingy to test your PHP Intellegence? Link to comment Share on other sites More sharing options...
cooldude832 Posted December 13, 2007 Share Posted December 13, 2007 yeah one of those thingies Link to comment Share on other sites More sharing options...
Crew-Portal Posted December 13, 2007 Share Posted December 13, 2007 yeah one of those thingies Woah! Dude thats an awesome idea! Link to comment Share on other sites More sharing options...
cooldude832 Posted December 13, 2007 Share Posted December 13, 2007 come up with questions, I figure it will need to have time limits put on each question also. Link to comment Share on other sites More sharing options...
Crew-Portal Posted December 13, 2007 Share Posted December 13, 2007 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 More sharing options...
cooldude832 Posted December 13, 2007 Share Posted December 13, 2007 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 More sharing options...
kenrbnsn Posted December 13, 2007 Share Posted December 13, 2007 Enough chit-chat that doesn't pertain to the subject. Topic locked. Ken Link to comment Share on other sites More sharing options...
Recommended Posts