I have a database with 5000 entries of local area codes to separate the area code from a phone number string. I'm not sure if the code I use is the fastest way to solve these job.
I shrink the number of db entries with the first three digits from phone number string. After this I put the entries found in db in an array and finally search these array for a match.
What do you think guys? Would you do it the same?
<?php
$phonenumber = '03522522492';
$country = 'DE';
$link = mysqli_connect('Host', 'DB', 'Pass','User') or die(mysqli_error());
$db_selected = mysqli_select_db( $link, 'DB');
$fristthree = substr($phonenumber,0,3);
$array = array();
$query = mysqli_query($link, "SELECT * FROM `Areacodes` WHERE `Country` LIKE '".$country."%' AND `Areacode` like '".$fristthree."%' ORDER BY CHAR_LENGTH(Areacode) ASC");
while($row = mysqli_fetch_assoc($query)){
$array[] = $row;
}
foreach ($array as $areacode) {
$subString = substr($phonenumber, 0, strlen($areacode["Areacode"]));
if ($subString == $areacode["Areacode"]) {
$phone = $subString." ".substr($phonenumber, strlen($areacode["Areacode"]));
$location = $areacode["Location"];
}
}
if (!empty($phone)) {
echo $phone;
echo '<br><br>';
echo $subString;
echo '<br><br>';
echo $location;
}
else {
echo "No Areacode found.";
}
?>