Jump to content

Can Anyone solve this?


don4of4

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/203400-can-anyone-solve-this/
Share on other sites

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];
}

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];

 

   

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;

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 />";
?>

Archived

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