Polski support Kohany
No i stało się – tak się zapaliłem do Kohany, że postanowiłem wystartować z polskim supportem do tego świetnego, choć ciągle rozwijającego się frameworka. Strona supportu jest cały czas w tłumaczeniu, ale udało mi...
No i stało się – tak się zapaliłem do Kohany, że postanowiłem wystartować z polskim supportem do tego świetnego, choć ciągle rozwijającego się frameworka. Strona supportu jest cały czas w tłumaczeniu, ale udało mi...
Malutka funkcja, która się przydaje, np.: przy skracaniu tekstu artykułu, newsa, itp., gdzie mogą wystąpić niezamknięte tagi.
|
1 |
<br/><?<br/>function close_tags($html){<br/> #umieszcza wszystkie otwarte tagi w tablicy<br/> preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU",$html,$result);<br/> $openedtags=$result[1];<br/><br/> #umieszcza wszystkie zamknięte tagi w tablicy<br/> preg_match_all("#</([a-z]+)>#iU",$html,$result);<br/> $closedtags=$result[1];<br/> $len_opened = count($openedtags);<br/> # wszystkie tagi są zamknięte<br/> if(count($closedtags) == $len_opened){<br/> return $html;<br/> }<br/><br/> $openedtags = array_reverse($openedtags);<br/> # zamykanie tagów<br/> for($i=0;$i < $len_opened;$i++) {<br/> if (!in_array($openedtags[$i],$closedtags)){<br/> $html .= '</'.$openedtags[$i].'>';<br/> } else {<br/> unset($closedtags[array_search($openedtags[$i],$closedtags)]);<br/> }<br/> }<br/> return $html;<br/>}<br/>?> |
Kolejna klasa, która wpadła w moje ręce, kiedy poszukiwałem rozwiązania do zapisywania danych do pliku XML. Oczywiście sprawa byłaby prostsza, gdyby serwer obslugiwał SimpleXML, ale tak nie było. Dlatego potrzebne było inne rozwiązanie.
|
1 |
<br/><?php<br/><br/>function & XML_unserialize(&$xml){<br/> $xml_parser = &new XML();<br/> $data = &$xml_parser->parse($xml);<br/> $xml_parser->destruct();<br/> return $data;<br/>}<br/><br/>function & XML_serialize(&$data, $level = 0, $prior_key = NULL){<br/> if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"n"; }<br/> while(list($key, $value) = each($data))<br/> if(!strpos($key, ' attr')) #if it's not an attribute<br/> #we don't treat attributes by themselves, so for an empty element<br/> # that has attributes you still need to set the element to NULL<br/><br/> if(is_array($value) and array_key_exists(0, $value)){<br/> XML_serialize($value, $level, $key);<br/> }else{<br/> $tag = $prior_key ? $prior_key : $key;<br/> echo str_repeat("t", $level),'<',$tag;<br/> if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element<br/> while(list($attr_name, $attr_value) = each($data["$key attr"]))<br/> echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';<br/> reset($data["$key attr"]);<br/> }<br/><br/> if(is_null($value)) echo " />n";<br/> elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>n";<br/> else echo ">n",XML_serialize($value, $level+1),str_repeat("t", $level),"</$tag>n";<br/> }<br/> reset($data);<br/> if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }<br/>}<br/><br/>class XML{<br/> var $parser; #a reference to the XML parser<br/> var $document; #the entire XML structure built up so far<br/> var $parent; #a pointer to the current parent - the parent will be an array<br/> var $stack; #a stack of the most recent parent at each nesting level<br/> var $last_opened_tag; #keeps track of the last tag opened.<br/><br/> function XML(){<br/> $this->parser = &xml_parser_create();<br/> xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, false);<br/> xml_set_object(&$this->parser, &$this);<br/> xml_set_element_handler(&$this->parser, 'open','close');<br/> xml_set_character_data_handler(&$this->parser, 'data');<br/> }<br/> function destruct(){ xml_parser_free(&$this->parser); }<br/> function & parse(&$data){<br/> $this->document = array();<br/> $this->stack = array();<br/> $this->parent = &$this->document;<br/> return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL;<br/> }<br/> function open(&$parser, $tag, $attributes){<br/> $this->data = ''; #stores temporary cdata<br/> $this->last_opened_tag = $tag;<br/> if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before<br/> if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric<br/> #this is the third or later instance of $tag we've come across<br/> $key = count_numeric_items($this->parent[$tag]);<br/> }else{<br/> #this is the second instance of $tag that we've seen. shift around<br/> if(array_key_exists("$tag attr",$this->parent)){<br/> $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);<br/> unset($this->parent["$tag attr"]);<br/> }else{<br/> $arr = array(&$this->parent[$tag]);<br/> }<br/> $this->parent[$tag] = &$arr;<br/> $key = 1;<br/> }<br/> $this->parent = &$this->parent[$tag];<br/> }else{<br/> $key = $tag;<br/> }<br/> if($attributes) $this->parent["$key attr"] = $attributes;<br/> $this->parent = &$this->parent[$key];<br/> $this->stack[] = &$this->parent;<br/> }<br/> function data(&$parser, $data){<br/> if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags<br/> $this->data .= $data;<br/> }<br/> function close(&$parser, $tag){<br/> if($this->last_opened_tag == $tag){<br/> $this->parent = $this->data;<br/> $this->last_opened_tag = NULL;<br/> }<br/> array_pop($this->stack);<br/> if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];<br/> }<br/>}<br/>function count_numeric_items(&$array){<br/> return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;<br/>}<br/>?> |
Do sprawdzenia, jaki czas jest potrzebny, aby nasza strona się wygenerowała, wydłubałem małą funkcję. Podajemy jej parametr, jeden na początku kodu strony, drugi na samym końcu.
|
1 |
<br/><?php<br/>function getmicrotime(){<br/> list($usec, $sec) = explode(" ",microtime());<br/> return ((float)$usec + (float)$sec);<br/> }<br/>function pagetime($type)<br/>{<br/> static $orig_time;<br/> if($type=="init")<br/> {<br/> $orig_time=getmicrotime();<br/> }<br/> if($type=="print")<br/> {<br/> printf("Page generated in %2.4f Seconds",getmicrotime()-$orig_time);<br/> }<br/>}<br/><br/>pagetime('init');<br/><br/>// ----------------- tutaj kod strony ------------------------------<br/><br/>pagetime('print');<br/>?> |
Kolejne wyzwanie i kolejne rozwiązania. Otóż, aby przeparsować stronę w poszukiwaniu wszystkich url’i, które się na niej znajdują, wystarczy użyć rozszerzenia (bliblioteki), która nie jest standardową biblioteką załączaną (włączaną) na serwerach hostingowych. Ale jak...
Po wielu poszukiwaniach prostej klasy do generowania "Exceli" znalazłem takową, i okazało się, że się sprawdza (przynajmniej mi się sprawdziła). Klasa generuje kod XLSa na podstawie mutitablicy.
|
1 |
<br/> <?php<br/>class Excel<br/>{<br/> function xlsBOF() {<br/> $data = pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); <br/> return $data;<br/> }<br/> <br/> function xlsEOF() {<br/> $data = pack("ss", 0x0A, 0x00);<br/> return $data;<br/> }<br/> <br/> function xlsWriteNumber($Row, $Col, $Value) {<br/> $data = pack("sssss", 0x203, 14, $Row, $Col, 0x0);<br/> $data .= pack("d", $Value);<br/> return $data;<br/> }<br/> <br/> function xlsWriteLabel($Row, $Col, $Value ) {<br/> $L = strlen($Value);<br/> $data = pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);<br/> $data .= $Value;<br/> return $data;<br/> }<br/> <br/> function generateXLS($array)<br/> {<br/> $data = $this->xlsBOF();<br/> $amount = count($array);<br/> for($i=0;$i<$amount;$i++)<br/> {<br/> $row = $array[$i];<br/> $amount_row = count($row);<br/> for($j=0;$j<$amount_row;$j++)<br/> {<br/> if((int)$row[$j])<br/> {<br/> $data .= $this->xlsWriteNumber($i,$j,$row[$j]);<br/> }<br/> else<br/> {<br/> $data .= $this->xlsWriteLabel($i,$j,$row[$j]);<br/> }<br/> }<br/> }<br/> $data .= $this->xlsEOF();<br/> header("Pragma: public");<br/> header("Expires: 0");<br/> header("Cache-Control: must-revalidate, post-check=0, pre-check=0");<br/> header("Content-Type: application/force-download");<br/> header("Content-Type: application/octet-stream");<br/> header("Content-Type: application/download");<br/> header("Content-Disposition: attachment;filename=$filename.xls ");<br/> header("Content-Transfer-Encoding: binary ");<br/> echo $data;;<br/> }<br/>}<br/>?> |
Ostatnio przyszło mi stworzyć funkcję walidującą numer PESEL. Oczywiście najpierw pogrzebałem w encyklopedii (można ją znaleźć pod adresem www.google.pl ), and viola! Oto kod:
|
1 |
<br/>function sprawdzPESEL($str)<br/>{<br/> if (strlen($str) != 11 || !is_numeric($str)) //sprawdzamy czy podany numer ma 11 znaków<br/> {<br/> return false;<br/> }<br/><br/> $arrSteps = array(1, 3, 7, 9, 1, 3, 7, 9, 1, 3); // tablica z odpowiednimi wagami<br/> $intSum = 0;<br/> for ($i = 0; $i < 10; $i++)<br/> {<br/> $intSum += $arrSteps[$i] * $str[$i]; //mnożymy każdy ze znaków przez wagę i sumujemy wszystko<br/> }<br/> $int = 10 - $intSum % 10; //obliczamy sumę kontrolną<br/> $intControlNr = ($int == 10)?0:$int;<br/> if ($intControlNr == $str[10]) //sprawdzamy czy taka sama suma kontrolna jest w ciągu<br/> {<br/> return true;<br/> }<br/> return false;<br/>} ?> |