Thursday, September 6, 2012

Array pagination

function pagination_array($array, $page = 1, $link_prefix = false, $link_suffix = false, $limit_page = 12, $limit_number = 10)
{
    if (empty($page) or !$limit_page) $page = 1;

    $num_rows = count($array);


    if (!$num_rows or $limit_page >= $num_rows) return false;
   
    $num_pages = ceil($num_rows / $limit_page);
   
    $page_offset = ($page - 1) * $limit_page;

    //Calculating the first number to show.
    /*if ($limit_number)
    {
        $limit_number_start = $page - ceil($limit_number / 2);
        $limit_number_end = ceil($page + $limit_number / 2) - 1;
        if ($limit_number_start < 1) $limit_number_start = 1;
        //In case if the current page is at the beginning.
        $dif = ($limit_number_end - $limit_number_start);
        if ($dif < $limit_number) $limit_number_end = $limit_number_end + ($limit_number - ($dif + 1));
        if ($limit_number_end > $num_pages) $limit_number_end = $num_pages;
        //In case if the current page is at the ending.
        $dif = ($limit_number_end - $limit_number_start);
        if ($limit_number_start < 1) $limit_number_start = 1;
    }
    else
    {
        $limit_number_start = 1;
        $limit_number_end = $num_pages;
    }
    */

    //Generating page links.
/*    for ($i = $limit_number_start; $i <= $limit_number_end; $i++)
    {
        $page_cur = "$i";
        if ($page == $i) $page_cur = "$i";
        else $page_cur = "$i";

        $panel .= " $page_cur";
    }
    */

/*
    $panel = trim($panel);
    //Navigation arrows.
    if ($limit_number_start > 1) $panel = "<<  < $panel";
    if ($limit_number_end < $num_pages) $panel = "$panel > >>";

    $output['panel'] = $panel; //Panel HTML source.
    $output['offset'] = $page_offset; //Current page number.
    $output['limit'] = $limit_page; //Number of resuts per page.
*/

    $output['array'] = array_slice($array, $page_offset, $limit_page, true); //Array of current page results.
  
    return $output;
}

UTF8 Wordwrap

header("Content-Type: text/html; charset=UTF-8");


function wordwrap_utf8($str, $len)

    preg_match_all('/[\xE0-\xFF][\x80-\xFF]{2}|./', $str, $match);

    $finalStr  = ' ';

    $m            =    $match[0]; 

    $ret        =    array();
  
    $Total_Length  =  count($match[0]);

    $flag1 = 1;

    $flag2 = true;


    for($j=0; $j < $Total_Length; $j++)
    {
                $ret[]    =    $m[$j];           

                    if( $flag1 == $len )
                    {
                        $finalStr .= join('', $ret)."
";

                        $ret    = array();

                        $flag1 = 1;

                        $flag2 = false;
                    }
                    else if(  ( $j == $Total_Length-1 )  &&  ( $flag2 )  )
                    {
                        $finalStr .= join('', $ret)."
";                       
                    }
                    else
                    {
                        $flag1 = $flag1+1;
                        $flag2 = true;
                    }
    }

    return $finalStr;
}


$str ="aaaaaaaadsfdsffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdsfghgfhgggggggggggggffffffffffffffdddddddddddddaaaaaaaadsfdsffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdsfghgfhgggggggggggggffffffffffffffdddddddddddddaaaaaaaadsfdsffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdsfghgfhgggggggggggggffffffffffffffdddddddddddddaaaaaaaadsfdsffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdsfghgfhgggggggggggggffffffffffffffdddddddddddddaaaaaaaadsfdsffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdsfghgfhgggggggggggggffffffffffffffdddddddddddddaaaaaaaadsfdsffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdsfghgfhgggggggggggggffffffffffffffddddddddddddd";

$str = 'ㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹㄹ';

$width  = 10;

$test1 =  wordwrap_utf8($str, $width);

echo $test1;

?>

UTF8 String Split

function strcut_utf8($str, $len, $checkmb=false, $tail='')
{
    /**
     * UTF-8 Format
     * 0xxxxxxx = ASCII, 110xxxxx 10xxxxxx or 1110xxxx 10xxxxxx 10xxxxxx
     * latin, greek, cyrillic, coptic, armenian, hebrew, arab characters consist of 2bytes
     * BMP(Basic Mulitilingual Plane) including Hangul, Japanese consist of 3bytes
     **/
    $html_decimalcodes = array("&#8203;","&#9664;");

    preg_match_all('/[\xE0-\xFF][\x80-\xFF]{2}|./', $str, $match); // target for BMP

    $m            =    $match[0];
    $slen        =    strlen($str);    // length of source string
    $tlen        =    strlen($tail);    // length of tail string
    $mlen        =    count($m);        // length of matched characters

    if ($slen <= $len)
    {
            /*echo $str;
            echo '
';*/
            $str = str_replace($html_decimalcodes,"", $str);
            /*echo $str;
            echo '
';*/
            return $str;
    }
   
    if (!$checkmb && $mlen <= $len)
    {
            /*echo $str;
            echo '
';*/
            $str = str_replace($html_decimalcodes,"", $str);
            /*echo $str;
            echo '
';*/
            return $str;
    }


    $ret        =    array();
    $count        =    0;
    for($i=0; $i < $len; $i++) {
        $count += ($checkmb && strlen($m[$i]) > 1)?2:1;
        if($count + $tlen > $len) break;
        $ret[]    =    $m[$i];
    }

    $text        =    html_entity_decode(join('', $ret).$tail);


    /*echo $text;
    echo '
';*/
    $text = str_replace($html_decimalcodes,"", $text);
    /*echo $text;
    echo '
';*/

    return $text;
}

XML to Array

function XMLtoArrayFormat($XML)
{
    $xml_parser = xml_parser_create();
    xml_parse_into_struct($xml_parser, $XML, $vals);
    xml_parser_free($xml_parser);
    // wyznaczamy tablice z powtarzajacymi sie tagami na tym samym poziomie
    $_tmp='';
    foreach ($vals as $xml_elem) {
        $x_tag=$xml_elem['tag'];
        $x_level=$xml_elem['level'];
        $x_type=$xml_elem['type'];
        if ($x_level!=1 && $x_type == 'close') {
            if (isset($multi_key[$x_tag][$x_level]))
                $multi_key[$x_tag][$x_level]=1;
            else
                $multi_key[$x_tag][$x_level]=0;
        }
        if ($x_level!=1 && $x_type == 'complete') {
            if ($_tmp==$x_tag)
                $multi_key[$x_tag][$x_level]=1;
            $_tmp=$x_tag;
        }
    }
    // jedziemy po tablicy
    foreach ($vals as $xml_elem) {
        $x_tag=$xml_elem['tag'];
        $x_level=$xml_elem['level'];
        $x_type=$xml_elem['type'];
        if ($x_type == 'open')
            $level[$x_level] = $x_tag;
        $start_level = 1;
        $php_stmt = '$xml_array';
        if ($x_type=='close' && $x_level!=1)
            $multi_key[$x_tag][$x_level]++;
        while ($start_level < $x_level) {
            $php_stmt .= '[$level['.$start_level.']]';
            if (isset($multi_key[$level[$start_level]][$start_level]) && $multi_key[$level[$start_level]][$start_level])
                $php_stmt .= '['.($multi_key[$level[$start_level]][$start_level]-1).']';
            $start_level++;
        }
        $add='';
        if (isset($multi_key[$x_tag][$x_level]) && $multi_key[$x_tag][$x_level] && ($x_type=='open' || $x_type=='complete')) {
            if (!isset($multi_key2[$x_tag][$x_level]))
                $multi_key2[$x_tag][$x_level]=0;
            else
                $multi_key2[$x_tag][$x_level]++;
            $add='['.$multi_key2[$x_tag][$x_level].']';
        }
        if (isset($xml_elem['value']) && trim($xml_elem['value'])!='' && !array_key_exists('attributes', $xml_elem)) {
            if ($x_type == 'open')
                $php_stmt_main=$php_stmt.'[$x_type]'.$add.'[\'content\'] = $xml_elem[\'value\'];';
            else
                $php_stmt_main=$php_stmt.'[$x_tag]'.$add.' = $xml_elem[\'value\'];';
            eval($php_stmt_main);
        }
        if (array_key_exists('attributes', $xml_elem)) {
            if (isset($xml_elem['value'])) {
                $php_stmt_main=$php_stmt.'[$x_tag]'.$add.'[\'content\'] = $xml_elem[\'value\'];';
                eval($php_stmt_main);
            }
            foreach ($xml_elem['attributes'] as $key=>$value) {
                $php_stmt_att=$php_stmt.'[$x_tag]'.$add.'[$key] = $value;';
                eval($php_stmt_att);
            }
        }
    }
    return $xml_array;
}