don4of4 Posted May 31, 2010 Share Posted May 31, 2010 I just learned php and I can't find the error in this code which takes a mobile phone email address like "[email protected]" and outputs the carrier and number. I have the number (which was easy) but I want to use a loop to find the carrier. Currently, this code results in an infinite loop. { $User_Serv_Numb = explode("@", $User_Serv_Numb); $Rotator = 0; $Rotator1 = "vtext.com"; $Rotator1a = "Verizon"; $Rotator2 = "messaging.sprintpcs.com"; $Rotator2a = "Sprint"; $Rotator3 = "tmomail.net"; $Rotator3a = "T-Mobile"; $Rotator4 = "txt.att.net"; $Rotator4a = "AT&T"; $Rotator5 = "vmobl.com"; $Rotator5a = "Virgin Mobile"; while (Rotator < 6) { If ($Rotator == 5) { $Cur_Provider = "Unknown Carrier"; $Rotator = 6; } else { If ($User_Serv_Numb[1] == '$'.'Rotator'.$Rotator) { $Cur_Provider = '$'.'Rotator'.$Rotator.'a'; $Rotator = 6; } else { $Rotator = $Rotator + 1; } } } echo $Cur_Provider; $Number = $User_Serv_Numb[0]; $Cur_Number = $User_Serv_Numb[0]; } Thanks of your help! PS: I KNOW that using a loop isn't the easiest way. I just want to lean what I'm doing wrong. Quote Link to comment https://forums.phpfreaks.com/topic/203400-can-anyone-solve-this/ Share on other sites More sharing options...
riwan Posted May 31, 2010 Share Posted May 31, 2010 I would think this would fix your infinite loop while ($Rotator < 6) { Quote Link to comment https://forums.phpfreaks.com/topic/203400-can-anyone-solve-this/#findComment-1065545 Share on other sites More sharing options...
don4of4 Posted May 31, 2010 Author Share Posted May 31, 2010 I would think this would fix your infinite loop while ($Rotator < 6) { WOW! I just wasted 3 hour of my life. :'( Quote Link to comment https://forums.phpfreaks.com/topic/203400-can-anyone-solve-this/#findComment-1065546 Share on other sites More sharing options...
don4of4 Posted May 31, 2010 Author Share Posted May 31, 2010 Ok the loop is fixed, but it always returns Unknown Carrier... Im sure it has to do with the '.'s { $User_Serv_Numb = explode("@", $User_Serv_Numb); $Rotator = 0; $Rotator1 = "vtext.com"; $Rotator1a = "Verizon"; $Rotator2 = "messaging.sprintpcs.com"; $Rotator2a = "Sprint"; $Rotator3 = "tmomail.net"; $Rotator3a = "T-Mobile"; $Rotator4 = "txt.att.net"; $Rotator4a = "AT&T"; $Rotator5 = "vmobl.com"; $Rotator5a = "Virgin Mobile"; while ($Rotator < 6) { If ($Rotator == 5) { $Cur_Provider = "Unknown Carrier"; $Rotator = 6; } else { If ($User_Serv_Numb[1] == '$'.'Rotator'.$Rotator) //Is this right? I want it to be $RotatorNUMBER { $Cur_Provider = '$'.'Rotator'.$Rotator.'a'; $Rotator = 6; } else { $Rotator = $Rotator + 1; } } } echo $Cur_Provider; $Number = $User_Serv_Numb[0]; $Cur_Number = $User_Serv_Numb[0]; } Quote Link to comment https://forums.phpfreaks.com/topic/203400-can-anyone-solve-this/#findComment-1065547 Share on other sites More sharing options...
riwan Posted May 31, 2010 Share Posted May 31, 2010 You have several problem with your code. 1. The way you compare and setting value, you'll only compare to string '$Rotator1', '$Rotator2', etc (not comparing to the value of the variable) 2. $User_Serv_Numb should be set to lowercase 3. if the User_Serv_Numb is using vmobl.com you'll get Unknown carrier too. Try to fix all the things for you $User_Serv_Numb = explode("@", $User_Serv_Numb); $Rotator = 0; $Rotator1 = "vtext.com"; $Rotator1a = "Verizon"; $Rotator2 = "messaging.sprintpcs.com"; $Rotator2a = "Sprint"; $Rotator3 = "tmomail.net"; $Rotator3a = "T-Mobile"; $Rotator4 = "txt.att.net"; $Rotator4a = "AT&T"; $Rotator5 = "vmobl.com"; $Rotator5a = "Virgin Mobile"; $Cur_Provider = "Unknown Carrier"; while ($Rotator < 6) { If (strtolower($User_Serv_Numb[1]) == ${'Rotator'.$Rotator}) { $Cur_Provider = ${'Rotator'.$Rotator.'a'}; $Rotator = 6; } else { $Rotator = $Rotator + 1; } } echo $Cur_Provider; $Number = $User_Serv_Numb[0]; $Cur_Number = $User_Serv_Numb[0]; Quote Link to comment https://forums.phpfreaks.com/topic/203400-can-anyone-solve-this/#findComment-1065551 Share on other sites More sharing options...
ignace Posted May 31, 2010 Share Posted May 31, 2010 If I understand what you are trying to do, then this should do it: list($number, $carrier) = explode('@', $User_Serv_Numb); echo 'Carrier: ', $carrier, "<br>\n", 'Number: ', $number; Quote Link to comment https://forums.phpfreaks.com/topic/203400-can-anyone-solve-this/#findComment-1065600 Share on other sites More sharing options...
don4of4 Posted May 31, 2010 Author Share Posted May 31, 2010 Solved! Thank you riwan. Quote Link to comment https://forums.phpfreaks.com/topic/203400-can-anyone-solve-this/#findComment-1065650 Share on other sites More sharing options...
ignace Posted May 31, 2010 Share Posted May 31, 2010 Have you tried my code? As I am certain that this is what you were trying to do. And your code is just too complex for something so simple. If I understand what you are trying to do, then this should do it: list($number, $carrier) = explode('@', $User_Serv_Numb); echo 'Carrier: ', $carrier, "<br>\n", 'Number: ', $number; Quote Link to comment https://forums.phpfreaks.com/topic/203400-can-anyone-solve-this/#findComment-1065715 Share on other sites More sharing options...
PFMaBiSmAd Posted May 31, 2010 Share Posted May 31, 2010 Simple lookup tables are normally used for this kind of thing. Makes adding entries easier and the code is simpler and faster - <?php $User_Serv_Numb = "[email protected]"; // get your value from wherever it is at... $lookup = array(); $lookup["vtext.com"] = "Verizon"; $lookup["messaging.sprintpcs.com"] = "Sprint"; $lookup["tmomail.net"] = "T-Mobile"; $lookup["txt.att.net"] = "AT&T"; $lookup["vmobl.com"] = "Virgin Mobile"; // add more entries here ... list($number, $carrier) = explode("@", $User_Serv_Numb); $carrier = strtolower($carrier); if(isset($lookup[$carrier])){ $Cur_Provider = $lookup[$carrier]; } else { $Cur_Provider = "Unknown Carrier"; } echo "Number: $number, Carrier string: $carrier, Provider: $Cur_Provider<br />"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/203400-can-anyone-solve-this/#findComment-1065726 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.