Skip to content

Instantly share code, notes, and snippets.

@Bogdaan
Created April 17, 2015 08:19
Show Gist options
  • Save Bogdaan/f6292f69d5d3a0a47821 to your computer and use it in GitHub Desktop.
Save Bogdaan/f6292f69d5d3a0a47821 to your computer and use it in GitHub Desktop.
Code formatter
<?php
class PCode
{
private static $vector = array (
0 =>
array (
'detect' => '247\\d\\d\\d\\d',
'mask' => '+247-####',
),
// <<<<< more codes here
);
/**
*
* @param unknown $phs
* @return string
*/
public static function format($phn)
{
$result = $phn;
foreach(self::$vector as $row)
{
if(preg_match('/'.$row['detect'].'/s', $phn))
{
$reverse = array_reverse( str_split($phn) );
$reverseMask = array_reverse( str_split($row['mask']) );
$newresult = array();
$ridx = 0;
foreach($reverseMask as $k)
{
if($k=='#')
{
$newresult[] = $reverse[$ridx];
$ridx++;
}
else
$newresult[] = $k;
}
$result = implode('', array_reverse( $newresult ));
}
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment