Friday, February 17, 2012

egypt mobile number database fix

“Egyptian mobile phone numbers have all changed on Thursday October 6th, 2011 as part of a state drive to meet increased user demand”, the National Telecom Regulatory Authority has announced.

The changes mean every Egyptian mobile phone number will now be 11 digits in length.
Amendments are now different for each of Egypt's three mobile operators:
 Etisala
Current 10 digit numbers, which all start with 01, will take an extra 1. They will now begin with 011
Current 11 digit numbers, which all start with 0152, will change the 5 to a 1. They will now begin with 0112
 Mobinil 
Current 10 digit numbers, which start with 01, will add a 2. They will now begin with 012
Current 11 digit numbers, which start with 0150, will replace the 5 with a 2. They will begin with 0120
 Vodafone 
Current 10 digit numbers, which start with 01, will add a 0. They will now start with 010
Current 11 digit numbers, which start with 0151, will replace 5 with 0. They will start with 0101
Mobile operators will have four months to fully comply with the decision, during which time users will be able to call using either the old number or the amended one.



The following post can help you update your database with php. It edits numbers starting with +201XXXXXXXX that counts 12 and 01XXXXXXXX that counts 10 and it does handle +2015xxxxxxxx that counts 13 and 015xxxxxxxx that counts 11.
It does not mess with already updated numbers.
It only works on the pattern which means:
("it does not repair a number that is less or more than 10 characters and starts with 01",
"and it does not repair a number that is less or more than 12 characters and starts with+201")



then it sets every 11 character phone number that starts with 01XXXXXXXXX to +201XXXXXXXXX
You can change it to 00201XXXXXXXXX by setting the variable $format=true;

*****************************
*     HOW TO USE  THE FILE     *
*****************************
*********************************************************
1- Before using, please export & backup your table; for your own Data safety.
2- Make sure that the mobile field in your database supports at least 14 characters.
2- Add this file to your web server.
3- Modify the variables at the beginning of the file according to your case.
 To set:
To Set
Modify
Connection file
$dbconn value.
table name
$tb value
id column name
$id value
mobile column name
$mobile value
final format '00201XXXXXXXXX'
$format=true
final format '+201XXXXXXXXX'
$format=false
If you need to revise, a file is exported to the
named egyptmobilefix.txt to the directory of this file.

**************************************
default format'+201XXXXXXXXX'
to set final format '00201XXXXXXXXX' set variable $format=true;
to set final format '+201XXXXXXXXX' set variable $format=false;


Developed by: Mostafa El-Bakly
Alexandria, Egypt
You can contact me if you need any help
elbakly is my username at gmail.



or
  1. Copy and paste the following to your notepad, and save it as fixmobiles.php.
  2. Edit your variables to match your settings.
  3. Backup your database table.
  4. Upload the file to your server and run it.



<?php
/* comments and how to use developer details at the end of the php tag */
// please set your variables

//connection file name;
$dbconn="dbcon.php";
//table name
$tb='clients';
//id coloum name
$id='id';
//mobile coloum name
$mobile='mobile';
//format variable read line 40
$format=false;


include($dbconn);






mysql_query("UPDATE ".$tb." SET    ".$mobile." = replace(".$mobile.", '00', '+') WHERE  ".$mobile." LIKE '00%';");




$q=mysql_query("select * from ".$tb);
$filecontent='';
echo "<ul>";
while($fq=mysql_fetch_assoc($q)){
$mob=NULL;
//getting mobile numbers
$m=$fq['mobile'];
$nlen=strlen($m);

//finding the first 2 numbers
$m1=substr($m,0,2);

//making them all start with +201
if($m1=='+2'&&$nlen==12){
$m1="+2".'01';
$mr=substr($m,4);
$mx=substr($m,2);
$mob=$m1.$mr;
$mlen=strlen($mob);

}
if($nlen==13){

if(substr($m,0,5)=='+2015'){

$m1='+201';
$mr=substr($m,4);
$mob=$m1.$mr;

}
}
if($m1=='01' && $nlen==10){
$m1="+2".'01';
$mr=substr($m,2);
$mob=$m1.$mr;
$mlen=strlen($mob);

}
if($nlen==11){
if(substr($m,0,3)=='015'){
echo substr($m,0,3)."|".$m."<br/>";
$m1='+201';
$mr=substr($m,2);
$mob=$m1.$mr;

}

}
$error='';

if($mlen!=12){

$error="PLEASE check the original is ".$m." (". $nlen.") count is ".$mlen." of ";

}
if($mob){
$nm=substr($m1,0,2);

if($nm!='+2'){

echo "error mobile is ".$m."<br/>";
}
else{


//finding the 3rd number
$pnum=substr($mob,4,1);
switch($pnum){
case 0:
$fin=$m1.'0'.$mr;
break;
case 6:
$fin=$m1.'0'.$mr;
break;
case 9:
$fin=$m1.'0'.$mr;
break;
case 2:
$fin=$m1.'2'.$mr;
break;
case 7:
$fin=$m1.'2'.$mr;
break;
case 8:
$fin=$m1.'2'.$mr;
break;
case 1:
$fin=$m1.'1'.$mr;
break;
case 4:
$fin=$m1.'1'.$mr;
break;
case 5:
$npnum=substr($mob,5,1);
$mrn=substr($mr,1);

switch($npnum){
case 0:
$fin= $m1.'2'.$mrn;
break;
case 1:
$fin= $m1.'0'.$mrn;
break;
case 2:
$fin= $m1.'1'.$mrn;
break;

}


break;

}
$rt="orignal :".$m."(".$nlen.")";
if(strlen($fin)!=13){ $rt="orignal :".$m."(".$nlen.")";}



$p= "<li class='dd' ><font color='red'>".$error."</font><font color='green'> final: ".$fin."(".strlen($fin).") ".$rt. "and during modification: ".$mob ."| init :".$m1."|remaining :
".$mr."<br/>";
echo $p; $filecontent.=$p;
$cq="update ".$tb." set ".$mobile."='".$fin."' where ".$id."='".$fq[$id]."'";
$sq=  $cq."<br/></font></li>";
echo $sq; $filecontent.=$sq;
mysql_query($cq);
}
}

//while close
}

echo "</ul>";

// documenting actions
 $actionfile=fopen("egyptmobilefix.txt","w+");
fwrite($actionfile,$filecontent);
fclose($actionfile);

$q2=mysql_query("select * from ".$tb);
while($fq2=mysql_fetch_assoc($q2)){
$mq=$fq2[$mobile];
$lmq=strlen($mq);
$smq=substr($mq,0,2);
if($lmq==11&&$smq=='01'){
$fin='+2'.$mq;
$cq2="update ".$tb." set ".$mobile."='".$fin."' where ".$id."='".$fq2[$id]."'";

echo $cq2."<br/>";
mysql_query($cq2);

}
}

if($format==false){
mysql_query("UPDATE ".$tb." SET    ".$mobile." = replace(".$mobile.", '00', '+') WHERE  ".$mobile." LIKE '00%';");}
elseif($format==true){
mysql_query("UPDATE ".$tb." SET    ".$mobile." = replace(".$mobile.", '+', '00') WHERE  ".$mobile." LIKE '+%';");
}

/*

This is file updates mobile numbers in egypt after the October additional number with php, it only edits number starting with +201XXXXXXXX that counts 12 and 01XXXXXXXX that counts 10 and it does handle +2015xxxxxxxx that counts 13 and 015xxxxxxxx that counts 11.
it does not mess with already updated numbers.
it only works on the pattern which means:
("it does not repair number that is less or more than  10 characters and starts with 01",
"and it does not repair number that is less or more than 12 characters and starts with+201")



then it set every 11 character phone number that starts with 01XXXXXXXXX to +201XXXXXXXXX

                        ****************************
*  HOW TO USE  THE FILE    *
****************************
*********************************************************
1- Before using please export & backup your table; for your own Data safety.
2- make sure that the mobile field in your database supports at least 14 character
2- add this file to webserver
3- modify the variables in the beging of the file according to your case
 To set:
 - connection file name modify $dbconn value
 - table name modify $tb value
 - id column name modify $id value
 - mobile column name modify $mobile value

if you need to revise, a file is exported to the
named egyptmobilefix.txt to the directory of this file

**************************************
default format'+201XXXXXXXXX'
to set final format '00201XXXXXXXXX' set variable $format=true;
to set final format '+201XXXXXXXXX' set variable $format=false;

Developed by: Mostafa El-Bakly
Alexandria ,Egypt
you can contact me if you need any help
elbakly is my username at gmail
*/


?>

<style type="text/css" >
.dd{
border: 1px solid;
}
</style>