Elance PHP5 code test

function XmlRead($xmlstr)
{
static $res = ”;
$xml = new SimpleXMLELMNT($xmlstr);

if(count($xml->children()))
{
$res .= $xml->getName().PHP_EOL;
foreach($xml->children() as $child)
{
XmlRead($child->asXML());
}
}
else
{
$res .= $xml->getName().’: ‘.(string)$xml.PHP_EOL;
}

return $res;
}

  • SELECT id FROM user ORDER BY id DESC LIMIT 1,1
  • $res = array();
    while(list($checkbox,) = each($_POST))
    {
    $res[] = intval(substr($checkbox,strpos($checkbox,’_’) + 1));
    }
    sort($res);
    echo implode(‘ ‘,$res);

function GetUniqueOnes($arr)
{
$res = implode(‘,’,array_unique($arr));

return $res;
}
function GeneratePwd ($length,$chars)
{
$res = ”;
$char_length = strlen($chars);
for($i = 0; $i < $length; $i++)
{
$res .= $chars[rand(0,$char_length)];
}

return $res;
}

Split email addresses:
function SplitEmailAddrs($address)
{
list($user, $domain) = explode(‘@’,$address);
return array(‘user’ => $user, ‘domain’ => $domain);
}

function ReformatPhNumber($number)
{
if (preg_match(‘/^(\d[ -]?){7,12}$/’, $number, $matches))
{
return preg_replace(‘/[ -]/’, ”, $number);
}

throw new Exception(‘Invalid phone number’);
}

get longest string in arguments:
function GetLongestString(){
$length = 0;
foreach(func_get_args() as $arg)
{
$var = strlen($arg);
if($var > $length)
{
$length = $var;
}
}
return $length;
}

Find maximum in nested array:
function MaxArray($arr){
$GLOBALS[‘max’] = 0;
array_walk_recursive($arr,create_function(‘$item,$key’,’if($item > $GLOBALS[“max”]) $GLOBALS[“max”] = $item;’));
return $GLOBALS[‘max’];
}

output all numbers divisable by 8 from 200 to 600 :
for($i = 200; $i <= 592; $i+=8){ echo $i.’,’; }
echo $i;

Leave a Reply

Your email address will not be published. Required fields are marked *