Skip to content

Instantly share code, notes, and snippets.

@eugeneglova
Created April 5, 2013 17:42
Show Gist options
  • Save eugeneglova/5321161 to your computer and use it in GitHub Desktop.
Save eugeneglova/5321161 to your computer and use it in GitHub Desktop.
my version of oDesk PHP Programming Skills Test
<?php
// 1
$_POST['checkbox_2'] = true;
$_POST['checkbox_1'] = true;
$_POST['checkbox_3'] = true;
for ($_POST as $key => $val) {
if (preg_match('/checkbox_(\d+)/', $key, $matches)) {
echo $matches[1] . ' ';
}
}
// 2
for ($i = 200; $i <= 600; $i++) {
if ($i % 8 === 0) {
if ($i !== 200) {
echo ',';
}
echo $i;
}
}
// 4
function ReformatPhoneNumber($number) {
$valid = preg_match('/^\d[\d -]+\d$/', $number);
$digit_number = preg_replace('/\D/', '', $number);
$len = strlen($digit_number);
if ($valid && $len > 6 && $len < 13) {
return $digit_number;
} else {
throw new Exception('Invalid phone number');
}
}
echo ReformatPhoneNumber('012-345 69');
// echo ReformatPhoneNumber('012345');
// echo ReformatPhoneNumber('-012345 678');
// 5
function GeneratePassword($length, $alphaNum) {
$alphaNumArray = str_split($alphaNum);
$arrayLength = sizeof($alphaNumArray);
$returnString = '';
for ($i = 0; $i < $length; $i++) {
$index = rand(0, $arrayLength - 1);
$returnString .= $alphaNumArray[$index];
}
return $returnString;
}
echo GeneratePassword(5,'abc0123');
// echo GeneratePassword(7,'abczxc012394');
// 6
function ReadXml($xmlstr) {
if (preg_match_all('/<(\w+)[^>]*>(.*)<\/\1>/i', $xmlstr, $matches)) {
// var_dump($matches);
foreach ($matches[1] as $key => $value) {
$xml_value = $matches[2][$key];
if (preg_match('/</', $xml_value)) {
echo $value . PHP_EOL;
ReadXml($matches[2][$key]);
} else {
echo $value . ': ' . $xml_value . PHP_EOL;
}
}
}
}
$xmlstr= '<Address><to>James</to><from>Jani</from><heading>Reminder</heading><body>Please check your mail.</body></Address>';
echo ReadXml($xmlstr);
// 7
function GetUniqueOnes($arr) {
return implode(',', array_unique($arr));
}
$arr = array(34,54,67,68,141,151,161,141,54,151,54);
//34,54,67,68,141,151,161
echo GetUniqueOnes($arr);
// 8
function MaxArray($arr) {
return max(iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($arr))));
}
$arr = array(array(141,151,161), 2, 3, array(101, 202, array(303,404)));
echo MaxArray($arr);
// 9
function SplitEmailAddress($address) {
$addressArray = explode('@', $address);
return array(
'user' => $addressArray[0],
'domain' => $addressArray[1],
);
}
$arr = SplitEmailAddress('[email protected]');
var_dump($arr);
// 10
function callback($a) {
return strlen($a);
}
function GetLongestString() {
$args = func_get_args();
return max(array_map('callback', $args));
}
echo GetLongestString("a", "aaa", "aa");
echo GetLongestString("a", "bcd", "efgh", "ij", "");
@Sukonnik-Illia
Copy link

//4 you have a mistake in this answer
your code will pass phone numbers as '1--234567','1 -234567' and similar
My version is:
function ReformatPhoneNumber($number)
{
$pattern[0] = '/^\d[0-9\s-]+\d$/';
$pattern[1] = '/(-|\s)(-|\s)/';

if(preg_match ($pattern[0], $number) &&
!preg_match($pattern[1], $number))
{
$repl_number = preg_replace('/\D/', '', $number);
$len = strlen($repl_number);

  if ($len > 6 && $len < 13)
{
  return $repl_number;
}
}

else throw new Exeption('Invalid phone number');
}

@andreyart
Copy link

Here is valid ReformatPhoneNumber function

function ReformatPhoneNumber($number) {
    $numberValid = false;
    $patterns = array(
        '/^\d[0-9\s-]+\d$/',
        '/(-|\s)(-|\s)/'
    );
    if(preg_match ($patterns[0], $number) &&
        !preg_match($patterns[1], $number)) {
        $numericNumber = preg_replace('/\D/', '', $number);
        $numberLength = strlen($numericNumber);
        if ($numberLength > 6 && $numberLength < 13) {
            $numberValid = true;
        }
    }
    if($numberValid) {
        return $numericNumber;
    } else {
        throw new Exception('Invalid phone number');
    }
}

try {
    var_dump(ReformatPhoneNumber('012-345 69'));
}  catch (Exception $e) {
    echo $e->getMessage(), '<br/>';
}

try {
    var_dump(ReformatPhoneNumber('012345'));
}  catch (Exception $e) {
    echo $e->getMessage(), '<br/>';
}

try {
    var_dump(ReformatPhoneNumber('-012345 678'));
}  catch (Exception $e) {
    echo $e->getMessage(), '<br/>';
}

try {
    var_dump(ReformatPhoneNumber('01203- 34566'));
}  catch (Exception $e) {
    echo $e->getMessage(), '<br/>';
}

try {
    var_dump(ReformatPhoneNumber('123456678875432'));
}  catch (Exception $e) {
    echo $e->getMessage(), '<br/>';
}

try {
    var_dump(ReformatPhoneNumber('1234x567'));
}  catch (Exception $e) {
    echo $e->getMessage(), '<br/>';
}

@eugeneglova
Copy link
Author

Hey @Sukonnik-Illia @andreyart guys, just noticed your comments :-) thanks anyway for comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment