User Tools

Site Tools


programming:php

Multibyte UCFirst

function mb_ucfirst($string)
{
    return mb_strtoupper(mb_substr($string, 0, 1)).mb_strtolower(mb_substr($string, 1));
}
function find_closest_ip($my_ip, $ips) {
    $my_ip = ip2long($my_ip);
    $distances = array();
    foreach ($ips as $ip) {
        $distances[$ip] = ceil(log(($my_ip ^ ip2long($ip)) + 1, 2));
    }
    return array_search(min($distances), $distances);
}
function check_email($email) {
    $atom = '[-a-z0-9!#$%&\'*+/=?^_`{|}~]'; // znaky tvořící uživatelské jméno
    $domain = '[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])'; // jedna komponenta domény
    return eregi("^$atom+(\\.$atom+)*@($domain?\\.)+$domain\$", $email);
}

Number with leading zeros

sprintf('%02d', $month)

Import XML

$xmlendpoint = 'http://blabla/endpoint';

setlocale(LC_NUMERIC, 'C');

$file = file_get_contents($xmlendpoint, false);
$xml = new SimpleXMLElement($file);

foreach ($xml->neco as $neco) {
sql("INSERT INTO neco VALUES ('".$neco->typ."','".$neco->evcj."','".$neco->popis."')");
}
programming/php.txt · Last modified: 2018/01/02 15:55 by Jan Forman