This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Heap Sort. | |
| * | |
| * Keywords: heap | |
| * Time Complexity: O(n log n) | |
| * Space Complexity: O(1) | |
| */ | |
| class HeapSort extends SplHeap | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Searching a value from a sorted array with dichotomy method. | |
| * | |
| * Keywords: median | |
| * Time Complexity: O(log n) | |
| * Space Complexity: O(1) | |
| * | |
| * @param int $target The value you want to find. | |
| * @param array $sorted_array The sorted array you want to search. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Swap two variables' values without extra variable. | |
| */ | |
| # Solution 1: | |
| function solution1 ($a, $b) | |
| { | |
| $a = $a + $b - ($b = $a); | |
| echo "solution1 :", PHP_EOL; | |
| var_dump($a, $b); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * xml字符串转成数组。 | |
| * | |
| * @param string $xml | |
| * @return array | |
| */ | |
| function xml2arr ($xml) | |
| { | |
| libxml_disable_entity_loader(true); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * 计算某年有多少周。按每周以星期一开始来计算。 | |
| * @param int $year | |
| * @return int | |
| */ | |
| function weekTotal($year) | |
| { | |
| /** | |
| * 计算某个日期是星期几。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * 二维数组(例如:从数据读取一个数据列表。)按两个字段进行排序。使用冒泡排序。 | |
| * | |
| * @param array $arr 待排序数组。 | |
| * @param array $ord 排序因子。 | |
| * 例子:[ | |
| * 0 => 'field1:asc', | |
| * 1 => 'field2:desc', | |
| * ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * 用正则将所有`UTF8`字符拿出来,扔进一个数组。 | |
| * 将数组的所有字符连接起来,构成结果字符串。 | |
| */ | |
| /** | |
| * 处理函数 | |
| * @param string $str 待过滤字符串。 | |
| * @return string | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * PHP三种POST数据的方式。 | |
| */ | |
| class PHPPost | |
| { | |
| /** | |
| * 通过cUrl扩展的POST。 | |
| * @param string $url | |
| * @param array $post_data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env php | |
| <?php | |
| // php.ini : `phar.readonly = Off` | |
| $longopts = array( | |
| 'name::', | |
| 'path::', | |
| 'init:' | |
| ); | |
| $opts = getopt('', $longopts); | |
| $name = (isset($opts['name'])) ? $opts['name'] . '.phar' : ''; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * json_decode() 如果返回 'null',说明触犯了以下规则: | |
| * 1. 包含非UTF-8编码的字符。 | |
| * 2. 在最后元素有逗号。 | |
| * 3. 使用单引号包含元素的值。 | |
| * 4. 包含了`ASCII`码的`0~31`代表的不可见符号。 | |
| * 5. 包含了微软编辑器特有的`BOM`头。 | |
| * | |
| * 关于第4、5点问题,可以用以下函数处理: |