Skip to content

Instantly share code, notes, and snippets.

@anova
Created July 27, 2017 10:47
Show Gist options
  • Save anova/0d341b7cee5f330522781ed2d581038c to your computer and use it in GitHub Desktop.
Save anova/0d341b7cee5f330522781ed2d581038c to your computer and use it in GitHub Desktop.
<?php
function phone_format( $phone )
{
$retVal = $phone;
//telefon ile ilgili islemler
if (0 === strlen($retVal)) {//telefon numarasi yoksa hic ugrasma
return $retVal;
}
//tireleri kaldır
$retVal = str_replace('-', '', $retVal);
//boşlukları kaldır
$retVal = str_replace(' ', '', $retVal);
//istanbul avrupa yakasi telefonlarinda alan kodu yazilmadigi oluyor.
//bu durumu kontrol edip duzenleyen kod
if (7 === strlen($retVal)) {
$retVal = '0212' . $retVal;
}
//başında 0 yok ise 0 ekle
if (strpos($retVal, '0') !== 0) {
$retVal = '0' . $retVal;
}
//11 karakterden fazla ise, ilk 11 karakteri al
if (strlen($retVal) > 11) {
$retVal = mb_substr($retVal, 0, 11);
}
//bosluklar ekle ( https://stackoverflow.com/a/4708314 )
if (preg_match('/^(\d{4})(\d{3})(\d{2})(\d{2})$/', $retVal, $matches)):
array_shift($matches);
$retVal = implode(' ', $matches);
endif;
return $retVal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment