User Tools

Site Tools


programming:php

PHP Fiddle

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."')");
}

Sort Array by key

foreach($xml->sap as $id) {
	$znakSort[] = $id->znak;
}
array_multisort($znakSort, SORT_ASC, SORT_STRING, $xml->sap);

Input filtering (UTF8)

function filterinput($string)
{
    preg_match_all("/[\@\\.\s\w\p{L}\p{N}\p{Pd}]/u", $string, $result);
    return implode('', $result[0]);
}
programming/php.txt · Last modified: 2018/05/22 18:11 by Jan Forman